View Single Post
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 453 Times in 451 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