Jump to content

Pausing Between Re-executing a loop (do...while)


Leither
 Share

Recommended Posts

Hi! So, I'm trying to make a countdown until my page redirects in five seconds. This is what I have:

var i = 6function countmake() {i--;var y = i + " seconds left until redirection."; var x = document.getElementById('timespan').innerHTML = y;}function countdown() {do{var zzz = setTimeout("countmake()", 1000);break;}while(i > 0);}

Which executes when the body loads in (onLoad). I have the break statement there because there's an error when the function executes. I think the error occurs because even though the function has a timeout the do...while loop tries to execute the function until i hits 0 and so it pops off the code as many times as it can in 1 second causing a bog down. I've checked the tutorials, and I don't see any way I can set a 1 second pause in between each attempted loop. Does anyone have a solution for this? (Also, I know I could do a function where I simply do 5...4...etc. and setTimeout to where they activate at the correct time, but I still would like to know if there's a solution where I can use the function I just set forward.) Thanks for looking.

Link to comment
Share on other sites

Why not just have the entire loop controlled by the setTimeout() ?

i = 6;function countdown() {i--;var y = i + " seconds left until redirection.";var x = document.getElementById('timespan').innerHTML = y;if (i > 0) setTimeout("countdown()", 1000);}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...