SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Dublin
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Time delay on a function

    Hi I want to put a time delay on a function. This is how I'm going implementing it, however it's not working......


    function loadXMLDoc()
    {
    setTimeout('loadXMLDoc()',5000);
    //code
    ....
    ..

    ..
    .
    }

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Try moving the set|Timeout after the rest of the code (assuming you want the code to constantly repeat with the delay in between) or after the function completely if you just want it to run once after the delay.

    Code:
    function loadXMLDoc()
    {
    //code
    ....
    ..
    
    ..
    .
    setTimeout(loadXMLDoc,5000);
    }
    or

    Code:
    function loadXMLDoc()
    {
    //code
    ....
    ..
    
    ..
    .
    }
    setTimeout(loadXMLDoc,5000);
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Dublin
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Stephen, yes I tried these earlier, but it didn't work

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Dublin
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oddly enough when I try this code below, the submission of the button goes out of control

    Code:
    function loadXMLDoc()
    {
    //code
    ....
    ..
    
    ..
    .
    setTimeout(loadXMLDoc,5000);
    }

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    How about using setInterval instead?

    Code javascript:
    function loadXMLDoc()
    {
    //code
    ....
    ..
     
    ..
    .
    }
    setInterval(loadXMLDoc, 5000);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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