+ 8

This code isn't working..[closed]

Sry if this isn't right to post here.. But my code written in JavaScript isn't working.. How i want it to work is.. The box should go on moving from left to right.. But it stays at the start! Is this because of the setInterval function? How can i fix it? https://code.sololearn.com/W7sIA3CuvCwE/?ref=app

14th Nov 2017, 8:43 AM
Yash✳️
Yash✳️ - avatar
7 odpowiedzi
14th Nov 2017, 10:16 AM
Uria
+ 4
@Uria I tried all that.. I added clearInterval(t2) in moveR's else statement so that it starts moving right.. But when is reaches 150px, since t2 is 'off' it again stays there.. Is there a function to turn the interval 'on' again?
14th Nov 2017, 9:00 AM
Yash✳️
Yash✳️ - avatar
+ 4
@uria I didn't really got what you said.. The vars are already out of the moveR/L functions(on the top)
14th Nov 2017, 10:07 AM
Yash✳️
Yash✳️ - avatar
+ 4
Niceeeeeeeee... You're the best!!! Now I'll try to move it fully ( L, R, up, down) :) Thanx again!
14th Nov 2017, 10:20 AM
Yash✳️
Yash✳️ - avatar
+ 1
remove t2, and clear t1 when it gets to pos=150. As it is now it moves in both directions at the same speed&time, so basically not moving.
14th Nov 2017, 8:52 AM
Uria
+ 1
Glad I could help :)
14th Nov 2017, 10:23 AM
Uria
0
@yash You can define the timers vars (t1,t2) outside of the moveL/R functions, then you can use clearInterval() or reassign it from those functions when the if condition is true. Note that you need to create both t1,t2 outside the moveL/R, then to initiate only the t1 so it starts to move. Let me know how it goes :)
14th Nov 2017, 9:48 AM
Uria