+ 2
Why my code doesnt work?
I want it to move from left to right infinitely by pressing button. I have no idea whats wrong. https://code.sololearn.com/WvNyATS3B7TL/?ref=app
11 Réponses
+ 1
a way to do it:
var goingRight;
var arrow;
var arrowPos;
window.onload = function(){
goingRight = true;
arrowPos = 170;
arrow = document.getElementById("arrow");
loop();
}
function loop(){
if(goingRight){
arrowPos += 1;
if(arrowPos > 340){
goingRight = false;
}
}else{
arrowPos -= 1;
if(arrowPos < 0){
goingRight = true;
}
}
arrow.style.left = arrowPos + "px";
requestAnimationFrame(loop);
}
+ 2
Scoozak I just wanted to say that he forgot put style before left attribute
+ 2
Scoozak i just wanna say i noticed one mistake i made and u didnt notice too: it goes to pos 340 but then stops!
Ez to fix: return arrow_move_left()
!! There were () missing!!
+ 1
That code is great but i have questions:
How often does it call function again?
Whats wrong with my code?
What would fix my code?
Is it possible without requestAnimationFrame(cuz idk that yet and i dont want to use unknown codes...)?
Thank you very much!
+ 1
https://code.sololearn.com/W16yun934NXD/?ref=app
It still doesnt work... :(
+ 1
It makes error
0
requestAnimationFrame calls a function 60 a sec
0
arrow = document.getElementById("arrow")
var arrow_pos = 170;
function arrow_move_right () {
arrow_pos += 1;
arrow.style.left = arrow_pos + "px";
if (arrow_pos == 340) {
return arrow_move_left()
}
setTimeout(arrow_move_right, 10);
};
function arrow_move_left () {
arrow_pos -= 1;
arrow.style.left = arrow_pos + "px";
if (arrow_pos == 0) {
return arrow_move_right()
}
setTimeout(arrow_move_left, 10);
};
//window.onload (arrow_move_right())
errors were:
-a html-elements position is accessed using style
-you forgot to call the function, you just returned the function name ...
0
you have to press start ...
0
really? for me it doesn't ...
what does it say? 😅
0
a a I think she was focused on error thats been corrupted