Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-03-2013, 10:43 PM   PM User | #1
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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>
xelawho is offline   Reply With Quote
Old 01-04-2013, 12:09 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
(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.
Old Pedant is offline   Reply With Quote
Old 01-04-2013, 12:31 AM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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.
xelawho is offline   Reply With Quote
Old 01-04-2013, 01:49 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
xelawho (01-04-2013)
Old 01-04-2013, 03:13 AM   PM User | #5
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
no, that's good, actually - thanks. It does exactly what I want instead of kind of
xelawho is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:37 AM.


Advertisement
Log in to turn off these ads.