...

View Full Version : setTimeout not working



Keleth
03-05-2012, 11:34 PM
So I have a very simple block of code:


function successfulReg() {
alert(1);
// $.colorbox({ html: '<p>Registration successful!</p>' });
}
if ($('#regSuc').size()) setTimeout('successfulReg', 1000);


If the regSuc item exists (1 second after the page loads), I want to bring up a lightbox. As I couldn't get this to work, I tried to get an alert to show up. This did not work either.

I changed the setTimeout to


if ($('#regSuc').size()) setTimeout('alert(1), 1000);

Which did work. And I tried successfulReg and succesfulReg(), neither of which worked. Am I missing some basic element to getting a timeout to work?

webdev1958
03-05-2012, 11:45 PM
setTimeout() expects a reference to a function for an input, not a string. If you input a string with the call to the function then setTimeout has to evaluate that string.

Try


setTimeout(successfulReg, 1000)But if the alert() is not working, that means the function is not even called in the first place.

Keleth
03-05-2012, 11:52 PM
Ok, yah, that works. I (for some reason) thought it references functions though a literal.

Philip M
03-06-2012, 08:51 AM
Ok, yah, that works. I (for some reason) thought it references functions though a literal.

setTimeout("successfulReg()", 1000);

That is an old (obsolete) usage which was necessary (I think) in IE versions <5. It still works, but is equivalent to eval() which is very inefficient.

Correct usage:-

setTimeout(successfulReg, 1000);


Durex Extra Safe Condoms 24-Pack.
Buy Now: 3.32
4 Used and New from 1.00.
In stock.
- Amazon



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum