Nohana Posted December 14, 2007 Share Posted December 14, 2007 Is there any simpler way to generate a wait time between events other than using setTimeout() and clearTimeout()?Take for instance the following example: --JAVASCRIPT--var count=100;function setCountdown() {if(count > 0 ) count--;setTimeout("setCountdown()", 1000);}--HTML--<input type="button" onclick="setCountdown()" /> Isn't calling the function over and over hard on the browser? What's more, if the user keeps clicking on the button, the countdown would go faster than it should and I'd need to add another line of code to prevent this. Isn't there a way to get the same result using a looping such as while or for statement? Link to comment Share on other sites More sharing options...
aspnetguy Posted December 14, 2007 Share Posted December 14, 2007 calling it every second is fine for the browser just don't go calling it every 1 millesecond :)That is the common way of doing a countdown. Loops will not wait they just execute as fast as they can. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 17, 2007 Share Posted December 17, 2007 setTimeout returns a value, you can use that value to check if they click the button again and clear the previous timeout if it's already been set before setting the new one (or don't set the new one at all). Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.