foo(); function foo() { for (var i = 0; i < 5; i++) { setTimeout( function(){ callingByTimeout(i) } ,100) } } function callingByTimeout(arg1) { alert(arg1) //output for first time : 5 //output for second time : 5 //output for third time : 5 //output for fourth time : 5 //output for fifth time : 5 }
var count = 0; function foo() { if (count<5) { alert(count); count++; var result = setTimeout("foo()",100); } } foo();
foo(); function foo(i) { for (var i = 0; i < 5; i++) { setTimeout( function (index) { return function () { callingByTimeout(index); } } (i) , 1000) } } function callingByTimeout(arg1) { alert(arg1) //output for first time : 0 //output for second time : 1 //output for third time : 2 //output for fourth time : 3 //output for fifth time : 4 }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)