+ 2
Help Javascript!
var test = ["Hello", "Hi"], i = 0; function _test(){ document.write(test[i]); i += 1; if(test[i] > test.length){ return; } setTimeout(_test, 2000); } _test(); Output: Hello hei undefined undefined undefined .... and so on..
8 Answers
+ 3
oh right.. its "i" not "test[i]" ... thanks KrOW and Ya'iko
+ 2
dend
Are you sure you used
setTimeout(), or you used setInterval()
+ 1
Ya'iko im very sure i use settimeout
+ 1
dend
Your program was in a way that lead to an infinite recursion.
Anyway, here's the correct code
https://code.sololearn.com/WSf0EytcLk8B/?ref=app
+ 1
nah.. its working btw just need to replace "test[i]" to "i" .. the _test(); within the function is not the problem bcuz i put some condition that will stop the function execution
+ 1
Your conditional is wrong.
Try this
var test = ["Hello", "Hi"], i = 0;
function _test(){
document.write(test[i]);
i += 1;
if(i >= test.length){
return;
}
setTimeout(_test, 2000);
}
_test();
https://code.sololearn.com/Wa2j5TBT8U8l/?ref=app
0
var test = ["Hello", "Hi"], i = 0;
function _test(){
if(i>=test.length) return;
document.write(test[i]);
i += 1;
setTimeout(_test, 2000);
}
_test();
0
ok its finish.. :D
https://code.sololearn.com/WgIjJdOzn2ff/?ref=app