0

I'm trying to write a script to update an iframe every second. This is what I have so far, and for some reason it loads for a little while and then displays the number 14541 +/- 50 or so. The words "something" and "something else" also never appear on the screen.

Is it stopping at 14541 because of some built-in browser safeguard against infinite loops or something? Why is the timer not working correctly?

var c = 0;
var t;
timer();
document.write("something");
function timer(){
    if(t) { window.clearTimeout(t) }
    update_preview();
    c++;
    t=setTimeout(timer(), 1000);
    document.write("something else");
}
function update_preview(){
    prev = window.open("","preview");
    prev.document.open();
    prev.document.writeln("<html><head><title>live preview window</title></head><body>");
    prev.document.writeln(c);
    prev.document.writeln("</body></html>");
    prev.document.close();
}

6 Answers 6

1

Use this,

t=setTimeout(function(){timer();}, 1000);

or

t=setTimeout(timer, 1000);

or

t=setTimeout("timer()", 1000);

The last one is an option that should not be used.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't call method timer as a parameter of function timer. You have to pass as parameter function timer:

setTimeout(timer,1000);

Dont use document.write is obsolete. Look at DOM and jQuery in order to manipulate with the page dynamically.

1 Comment

Can you elaborate on this? I fixed the timer and now somehow it works at 0, and then redirects to a new page where it prints "something else" over and over, and a popup comes up that displays the increasing number.
0

Remove the () after timer.

By putting timer(), you are calling the timer function immediately and assigning the return value (undefined, since you don't return anything in that function) to be the function executed when the timer runs out.

Instead, you should just put timer. This passes the function itself, rather than its return value, to be the function called when the timer runs out.

Also, for clarity, I would suggest putting the function timer() {...} bit before the initial call to timer(). JavaScript does this for you automatically (called "hoisting"), but it's easier to understand if you put the function definition first yourself.

Comments

0

On this line t=setTimeout(timer(), 1000); you are not passing timer to setTimout, you are passing the result of timer. You should use t=setTimeout(timer, 1000); instead.

Comments

0

It would be better to use setInterval instead of setTimeout.

Comments

0

The reason "something else" is not displaying is because when you're calling setTimeout and passing the function in, the timer function is processing immediately. The same thing is happening inside the function. So you're creating a non-terminating infinite recursive loop that will make your browser unresponsive.

Since you're running setTimeout on timer every time you run the timer function, which is every 1 second, instead of using setTimeout, you should use setInterval.

So your code would look like this:

var c = 0;
var t;
timer();

// call setInterval here to run the timer function every 1 second
t=setInterval('timer()', 1000);

document.write("something");
function timer(){
    if(t) { window.clearTimeout(t) }
    update_preview();
    c++;
    document.write("something else");
}
function update_preview(){
    prev = window.open("","preview");
    prev.document.open();
    prev.document.writeln("<html><head><title>live preview window</title></head><body>");
    prev.document.writeln(c);
    prev.document.writeln("</body></html>");
    prev.document.close();
}

So now instead of running setTimeout every 1 second, you're running setInterval once and it will process every 1 second.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.