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 12 of 12
  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    sleep function in javascript

    Please tell me about sleep function in javascript


    Regards,
    Manjeet

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    This is the setTimeout() method of the window object.
    Example:-
    setTimeout("alert('message')", 5000); // the delay is expressed in milliseconds

    Another example:-
    var number=0;
    function annoy() {
    num ++;
    alert (This is every ten-second message number: " + num);
    window setTimeout("annoy()", 10000); // every ten seconds
    }
    annoy ();

    Google will point you to many further explanations and examples, e.g.
    http://www.devguru.com/technologies/...ettimeout.html
    Last edited by Philip M; 08-08-2006 at 06:50 PM.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Note that setTimeout is an asynchronous call. Any following Javascript will be immediately run. Only the function called by the setTimeout will be delayed.

    Javascript has no equivalent to the sleep function found in most other languages.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Apr 2006
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I don't want other part of javascript to execute

    Hi,

    I making call to function of Ajax script. This runs parallel.
    Sometimes the function doesnot get executed fully.

    That's why I need sleep.

    Please suggest some method.

    Regards

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Well, instead of using request.open("uri", "GET", true) you could use request.open("uri", "GET", false) to make it a synchronous call and have the scripting environment wait until the document is received.

  • #6
    New Coder
    Join Date
    Apr 2006
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks very much,
    This reallly worked.
    My problem has got solved.

  • #7
    New Coder
    Join Date
    Apr 2006
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ajax works fine in IE but not in Netscape

    I changed my code to XmlHttp.open("GET", requestUrl, false);
    so the script becomes synchronous.

    It works fine in IE but not in Netscape

    The part of code is

    if(XmlHttp)
    {

    //Setting the event handler for the response
    XmlHttp.onreadystatechange = HandleResponse;


    //Initializes the request object with GET (METHOD of posting),
    //Request URL and sets the request as asynchronous.
    XmlHttp.open("GET", requestUrl, false);

    //Sends the request to server
    XmlHttp.send(null);
    }
    On making it synchronous HandleResponse function does not get triggered.

    Please suggest some method to trigger that function by keeping the script synchronous

  • #8
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Why would you need the readystatechange handler if the call is synchronous?


    xmlhttp.send("");
    //processing waits until the request has completed
    alert(xmlhttp.responseText); // this works fine!

  • #9
    New Coder
    Join Date
    Apr 2006
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I figured it myself as well

    Thank for the solution.

    Actually I had figured it myself as well.

  • #10
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I hav a same problem.
    when i use the XmlHttp.open("GET", requestUrl, false);
    the call back function not be called in firefox and safari.
    please provide the solution.


    Thanks

  • #11
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have a same kind of problem.when i use the XmlHttp.open("GET", requestUrl, false);
    statement,the callback function not be called in firefox and safari.
    please provide the solution.

    Thanks .

  • #12
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by jkd View Post
    Why would you need the readystatechange handler if the call is synchronous?
    there is a bug, thats why. some firefoxes doesn't like callbacks from synchronous ajax.

    you can really forgo the callback, or simply manually call it after xhr.send.

    probably easier to simply move the code from the onreadystatechange function to the parent codeblock, right after .send().
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Posting Permissions

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