January 19, 201412 yr So i got this code and what's the difference between using the variable speed and not i mean if i remove speed it does not make any difference but if i use speed it does not show up any difference? The one using the variable speed: (function() { var speed = 10, moveBox = function() { var el = document.getElementById("box"), left = el.offsetLeft, moveBy = 1; el.style.left = left + moveBy + "px"; if (left > 399) { clearTimeout(timer); } }; var timer = setInterval(moveBox, speed); }()); The one not using the variable speed: (function() { var moveBox = function() { var el = document.getElementById("box"), left = el.offsetLeft, moveBy = 1; el.style.left = left + moveBy + "px"; if (left > 399) { clearTimeout(timer); } }; var timer = setInterval(moveBox); }());
January 19, 201412 yr setInterval requires a delay as it's second parameter, without it the code may not run properly in some browsers.
January 29, 201412 yr You might want to try something like this: function movebox(speed) { setInterval(function(){ var el = document.getElementById("box"), left = el.offsetLeft, moveBy = 1; el.style.left = left + moveBy + "px"; }, speed); if (left > 399) { clearTimeout(timer); } } ...then call the function as you need it. Haven't checked to see if this works....
Create an account or sign in to comment