+ 2

Tell me the difference? Code 1 working 1 time & Code 2 working properly why?

Code 1 ------------------------------- var t = setInterval(move, 500); var box = document.getElementById("box"); function move() { var pos = 0; pos += 1; box.style.left = pos + "px"; } Code 2 ------------------------------- var t = setInterval(move, 500); var box = document.getElementById("box"); var pos = 0; function move() { pos += 1; box.style.left = pos + "px"; } Code 1 working 1 time & Code 2 working properly why?

13th Mar 2018, 9:16 AM
Mohamed Kalith
Mohamed Kalith - avatar
2 Antworten
+ 3
oh now I see why , "var pos=0" should not be in the move function in code1.everytime move is being called it is returning the value of pos back to 0 therefore the object isnt miving
13th Mar 2018, 9:52 AM
᠌᠌Code X
᠌᠌Code X - avatar
+ 1
you should upload the code,not texts of the code
13th Mar 2018, 9:51 AM
᠌᠌Code X
᠌᠌Code X - avatar