SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help for loop like sleep or wait function

    Hello folks ,

    I am working on project that requires some immediate help , i stuck in this could not proceed with next part for program

    Want to create delay in javascript loop like sleep or wait function

    sample -

    note:- setTimeout() not working
    for(var start = 1; start < 10; start++) {

    //wait for some minute

    }

    Thanks

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,000
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Welcome to the forums

    What exactly is not working for you?

    I would have thought something like this should do the job:

    Code JavaScript:
    for(var i = 1; i < 10; i++) {
      (function(i){
        setTimeout(function(){
          console.log("Hello " + i);
      }, 500 * i)
     })(i);
    }

  3. #3
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    I would have thought something like this would be more interesting

    Code:
    for(var i = 1; i < 10; i++) {
        (function(i){
            console.log("Hello " + i);
            i=i+10;
            console.log("Hello " + i);
        })(i);
    }

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,000
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    I shall fall on my sword

    Code JavaScript:
    for(var start = 1; start < 10; start++) {
      (function(i){
        setTimeout(function(){
          console.log("Hello " + i);
        }, 500 * i)
      })(start);
    }

    Thanks!

  5. #5
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Hehe, no need.

    Shadowing variables is tricky. The simple way JavaScript lets you write code often hides something a bit more complex. It pays to be more explicit about what's going on: passed by value function mechanism == implicit variable declarations (parameters), combined with variable shadowing.

    Add this to the closure concept involved in the first code and you got a solid list for head scratching.



    Without IIFE it should make more sense:

    Code:
    for(var counter = 1; counter < 10; counter++) {
      message(counter);
    }
    
    // it doesn't work in IE < 9
    //function message(ratio) {
    //    setTimeout( hello, 500 * ratio, ratio);
    //}
    
    // works everywhere
    function message(ratio) {
        setTimeout(hello.bind(null, ratio), 500 * ratio);
    }
    
    
    function hello(increment) {
        console.log("Hello " + increment);
    }


Tags for this Thread

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
  •