0

So I have the following code which i basically just a JSON string I am using eval to convert to an object. Now, this object has an array of elements that gets displayed to the screen via a for loop:

function DisplayListing(str)
{
    var obj = eval("(" + str + ")");
    var div = document.getElementById('Response');
    for(i=0; i<obj.files.length; i++)
    {
        div.innerHTML += '<span id="listing' + i + '" class="displayNone"><img src="' + obj.files[i].icon + '"/>' + obj.files[i].name + '</span><br />';   
    }   
}

This works just fine. However, what I want it to do is wait a set interval of time before it continues to the next element. I want to it basically call a function with a timeout, so each element fades onto the screen individually. All attempts so far on cause the last element to execute a function. Any help would be greatly appreciated!

3
  • I Know! It's an internal project. No outside risks. Commented Aug 30, 2011 at 12:39
  • Thanks for the warning though. Commented Aug 30, 2011 at 12:39
  • Eval is not always a bad idea, if the source is trusted and precautions are taken eval can be a nice and quick tool. But rarely can you trust a source so I would recommend using a json parser from json.org Commented Aug 30, 2011 at 12:41

2 Answers 2

3

http://jsfiddle.net/SfKNc/

var obj = {files: [1, 2, 3]}; // sample object - use JSON.parse by the way
var div = document.getElementById('Response');
for(var i=0; i<obj.files.length; i++) { // use var!
    setTimeout((function(i) {
        return function() { // i changes, so create a new function in which i does not change
            div.innerHTML += 
                '<span id="listing' + i + 
                '" class="displayNone">' + i + 
                '</span><br />';
        };
    })(i), i * 1000); // set timeout to 1000 ms for first item, 2000 for second etc.
}   
Sign up to request clarification or add additional context in comments.

3 Comments

This looks promising, however I am getting a syntax error: unexpected token around return function() {
@pimvdb: does this need to install N timers (N = obj.files.length) ?
@jiri: It currently does, yes.
0

you have manually create a sleep function something like the below:

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

or you create an empty function and use the setTimeout on it

  function sleep()
    {
    setTimeout(Func1, 3000);
    }

    Func1(){}

http://www.phpied.com/sleep-in-javascript/

2 Comments

Note that the infinite for loop causes the browser to hang, and might not work if looping 1e7 times finishes earlier (which is certainly possible as of today).
it was just to show an idea the source is the web site showed at the bottom.I personally prefer the second sleep function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.