JavaScript: Display array items after 3 seconds one a time -
i have code segment:
var = 0; (function loop() { text_objects[i].displaytext(); if (++i < text_objects.length) { settimeout(loop, 3000); } })();
which supposed display contents of array 1 @ time, separated 3 seconds. however, when run program, first item, , sort of freezes there, without updating , showing rest of items in array.
what doing wrong?
as correctly mentioned @nnnnnn,
loop() named function expression, , reference loop should in scope within function
also code works fine.
var = 0; var test = function(value){ this.text = value; } test.prototype.displaytext = function(){ document.write(this.text + "<br/>"); } var text_objects = []; (var j = 0; j<10; j++){ text_objects.push(new test(j)); } (function loop() { text_objects[i].displaytext(); if (++i < text_objects.length) { settimeout(loop, 3000); } })();
alternate approach:
var = 0; function loop() { document.write(i + "<br/>") if (++i < 5) { settimeout(loop, 3000); } } loop();
Comments
Post a Comment