Why the timer function doesn't work?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why the timer function doesn't work??

https://code.sololearn.com/WuUOAFfea7ju/?ref=app

11th Mar 2018, 12:22 PM
Sina
Sina - avatar
19 Answers
+ 7
No the timer works. I think you would like to stop definitively moving the box ?
13th Mar 2018, 6:03 PM
Chalza
Chalza - avatar
+ 7
The difference is ( in your code ) the timer will show 61 instead of 60.
13th Mar 2018, 6:04 PM
Chalza
Chalza - avatar
+ 6
@Sina var sec = 0; var num = 1; function repeat(){ var speeds=[Math.floor((Math.random()*50) + 80)]; var x = document.getElementById("mar"); x.scrollAmount=speeds; ++sec; if (sec>=60){ clearInterval(y) document.getElementById("mar").scrollAmount = 0; }
13th Mar 2018, 6:47 PM
Chalza
Chalza - avatar
+ 6
I added a var named sec wich is incremented every seconds
13th Mar 2018, 6:49 PM
Chalza
Chalza - avatar
+ 5
@Sina function timer(){ document.getElementById("p2").innerHTML = ++num if(num >=60){ clearInterval(x) } } var x = setInterval(timer,1000)
13th Mar 2018, 3:18 PM
Chalza
Chalza - avatar
+ 4
You saved y for the repeat function not for the timer. Your post is about the timer so I had not look at the repeat function. The clearInterval works but it just let a constant speed to the square. If you want to stop it you add to clear the interval and then to set the speed as 0
11th Mar 2018, 9:33 PM
Αητοιπe
Αητοιπe - avatar
+ 2
Yes but save it into a y variable
11th Mar 2018, 9:11 PM
Αητοιπe
Αητοιπe - avatar
+ 2
You have two variables named y
14th Mar 2018, 5:52 AM
Αητοιπe
Αητοιπe - avatar
+ 1
@Sina it works for me
11th Mar 2018, 12:43 PM
Αητοιπe
Αητοιπe - avatar
+ 1
the clearInterval doesn't work
11th Mar 2018, 12:45 PM
Sina
Sina - avatar
+ 1
@Sina sorry I didn't understand that's your problem. You clear an interval named y but the interval y does not exist. You just have to declare it using : var y = setInterval(timer, 1000); instead of : setInterval(timer, 1000);
11th Mar 2018, 12:53 PM
Αητοιπe
Αητοιπe - avatar
+ 1
there is that setInterval()
11th Mar 2018, 4:40 PM
Sina
Sina - avatar
+ 1
I saved it see
11th Mar 2018, 9:25 PM
Sina
Sina - avatar
+ 1
What id differnet between "++num" and " num++"??
13th Mar 2018, 3:42 PM
Sina
Sina - avatar
+ 1
See my code plz I added s.th to that and it doesn't work again
13th Mar 2018, 3:47 PM
Sina
Sina - avatar
+ 1
Yes I want to stop the box
13th Mar 2018, 6:41 PM
Sina
Sina - avatar
0
"What id differnet between "++num" and " num++"??"
17th Mar 2018, 2:06 PM
Arda Aydın
Arda Aydın - avatar
0
++num increases num by 1 and returns it
17th Mar 2018, 2:06 PM
Arda Aydın
Arda Aydın - avatar
0
num++ returns num and increases num by 1
17th Mar 2018, 2:06 PM
Arda Aydın
Arda Aydın - avatar