+ 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..

17th Jul 2018, 8:15 AM
dend
8 Answers
+ 3
oh right.. its "i" not "test[i]" ... thanks KrOW and Ya'iko
17th Jul 2018, 8:34 AM
dend
+ 2
dend Are you sure you used setTimeout(), or you used setInterval()
17th Jul 2018, 8:22 AM
Dlite
Dlite - avatar
+ 1
Ya'iko im very sure i use settimeout
17th Jul 2018, 8:23 AM
dend
+ 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
17th Jul 2018, 8:29 AM
Dlite
Dlite - avatar
+ 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
17th Jul 2018, 8:45 AM
dend
+ 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
17th Jul 2018, 9:27 AM
CalviŐ˛
CalviŐ˛ - avatar
0
var test = ["Hello", "Hi"], i = 0; function _test(){ if(i>=test.length) return; document.write(test[i]); i += 1; setTimeout(_test, 2000); } _test();
17th Jul 2018, 8:27 AM
KrOW
KrOW - avatar
17th Jul 2018, 10:53 AM
dend