+ 25
I NEED HELP WITH MY CODE :)
var x = 0; var t; window.onload = function() { var t = setInterval(aFunc, 1000) } function aFunc () { x++; if (x == 10) { clearInterval(t); } } So I have got this code... I have a more complicate code, but this is the part I struggle with... When x reaches 10, the function is still working, the interval is not stopping. Why is that happening?
7 Answers
+ 26
Remove var from line 4
+ 23
Oh yeah.... It works. Thanks... JS is so strange xD
+ 19
@Jafca I like your sarcasm đđ
+ 18
You had a global (I think) variable and then you made a new local variable in the function. So the two t's were different đ°
+ 17
@Shiv Mishra I'm glad you told @Gami again. I don't think he got the message the first time đ
+ 6
@Jafca yes, "var t" inside the function creates local variable without access from aFunc function
+ 6
put all the things in window.onload