Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts

    optimising an autosave function

    hello,

    I am writing a greasemonkey userscript to autosave a page. The below code works fine (no cross-browser dramas as the page will only ever be opened in firefox). This is what it's supposed to do:
    - a certain time (10 seconds here) after page load, the save button is clicked programatically, unless it is disabled (it becomes disabled when it saves and is only enabled once changes are made to the page).

    - The 10 second countdown starts again, either from the "else" in the clickIt function or from the onclick of the button.

    - If the save button is clicked manually, the timeOut is cleared and the 10 second countdown starts again.

    Anyway, like I say, the below code works fine - it just seems a little clunky. Can anybody see a better way of doing this?

    thanks in advance...

    Code:
    <body>
    <input type="button" value="save" id="store"/>
    <script>
    (function() {
    var auto_save_timer=setTimeout(clickIt,10000);
    
    function clickIt(){
    if (!document.getElementById("store").disabled){
    document.getElementById("store").click();
    	} else {
    auto_save_timer=setTimeout(clickIt,10000);	
    	}
    }
    
    document.getElementById("store").onclick=function() {
    alert("saved");
    if(auto_save_timer){
    clearTimeout(auto_save_timer)
    }
    auto_save_timer=setTimeout(clickIt,10000);
    };
    
    })();
    
    </script>
    </body>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    (it becomes disabled when it saves and is only enabled once changes are made to the page)
    That code isn't shown here. I assume it's independent of the grease monkey stuff?

    That code doesn't look too ugly, to me (well, except that your code indentations isn't pretty <grin/>).

    I think you could get away with doing it like this, though:
    Code:
    <script>
    (
      function() 
      {
          var button = document.getElementById("store");
    
          button.onclick = function(){ alert("saved"); } // optional
    
          function clickIt()
          {
              if ( ! button.disabled) { button.click( ); }
          }
          setInterval(clickIt,10000);
      }
    )();
    </script>
    After all, how long is a save going to take? Surely not more than 9.9 seconds?

    So why bother with clearing and setting the timeout over and over again?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Quote Originally Posted by Old Pedant View Post
    That code isn't shown here. I assume it's independent of the grease monkey stuff?
    Correct

    Quote Originally Posted by Old Pedant View Post
    So why bother with clearing and setting the timeout over and over again?
    That was in there to prevent the autosave going off right after a manual save had happened. You're right, though - a save takes a second or less, so it's not like it's a big deal sitting around like when Word autosaves. But it would still be nice to have the autosave happen x amount of time (it will probably be 5 minutes in reality, but that's irrelevant here - I know how to calculate that) after the last save, regardless of if it was an auto or manual.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Okay...minor mod:
    Code:
    <script>
    (
      function() 
      {
          var button = document.getElementById("store");
          var wait = setInterval(clickIt,10000);
    
          button.onclick = 
              function()
              { 
                  alert("saved"); // optional
                  clearInterval(wait);
                  wait = setInterval(clickIt,10000);
              };
    
          function clickIt()
          {
              if ( ! button.disabled) { button.click( ); }
          }
      }
    )();
    </script>
    But now that's not really different than your code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    xelawho (01-04-2013)

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    no, that's good, actually - thanks. It does exactly what I want instead of kind of


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •