+ 3
Why the timer function doesn't work??
19 ответов
+ 7
No the timer works. I think you would like to stop definitively moving the box ?
+ 7
The difference is ( in your code ) the timer will show 61 instead of 60.
+ 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;
}
+ 6
I added a var named sec wich is incremented every seconds
+ 5
@Sina
function timer(){
document.getElementById("p2").innerHTML = ++num
if(num >=60){
clearInterval(x)
}
}
var x = setInterval(timer,1000)
+ 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
+ 2
Yes but save it into a y variable
+ 2
You have two variables named y
+ 1
@Sina it works for me
+ 1
the clearInterval doesn't work
+ 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);
+ 1
there is that setInterval()
+ 1
I saved it see
+ 1
What id differnet between "++num" and " num++"??
+ 1
See my code plz I added s.th to that and it doesn't work again
+ 1
Yes I want to stop the box
0
"What id differnet between "++num" and " num++"??"
0
++num increases num by 1 and returns it
0
num++ returns num and increases num by 1