Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts

    setTimeout not working

    So I have a very simple block of code:

    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

    Code:
    		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?

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    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

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

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Ok, yah, that works. I (for some reason) thought it references functions though a literal.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by Keleth View Post
    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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    LinkBacks (?)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •