There is a layer:

<div style="position: absolute; left: 10px; top: 10px;" onClick="isMove(this)">Лол</div> 

There is a script:

 <script type="text/javascript"> function isMove(element) { for(t=0;t<10;t++){ setTimeout('actMove('+element+')', t*100); } } function actMove(element) { element.style.left=parseInt(element.style.left)+1+'px'; element.style.top=parseInt(element.style.top)+1+'px'; } </script> 

In fact, everything should work, this is passed normally to the first function, but the second one does not start.

PS if you change

 setTimeout('actMove('+element+')', t*100); 

on

 actMove(element); 

then everything works, but of course the situation changes instantly. How can I fix it?

  • I can assume the option: setTimeout (function () {actMove (element);}, t * 100); // add function - Specter

2 answers 2

You did not correctly call the function in the set interval, you need so

 function isMove(element) { for(t=0;t<10;t++){ setTimeout(function() { actMove(element) }, t*100); } } function actMove(element) { element.style.left=parseInt(element.style.left)+1+'px'; element.style.top=parseInt(element.style.top)+1+'px'; } 

And better to do this:

 function animate(obj) { var start = new Date(); var timer = setInterval(function() { var progress = (new Date() - start ) / obj.duration; if(progress > 1) progress = 1; obg.step(progress); if(progress == 1) clearInterval(timer); }, obj.delay) 

}

 var obj = { duration : 1000, delay : 10, step : function(progress) { elem.style.marginTop = 150 * progress + 'px'; } 

}

     <div id="myId" style="position: absolute; left: 10px; top: 10px;" onClick="isMove(this.id)">Лол</div> <script type="text/javascript"> function isMove(elementId) { for(t=0;t<10;t++){ setTimeout('actMove("'+elementId+'")', t*100); } } function actMove(elementId) { var el = document.getElementById(elementId); el.style.left=parseInt(el.style.left)+1+'px'; el.style.top=parseInt(el.style.top)+1+'px'; } </script> 

    "Why ID" - because you are trying to time out something like " actMove(HTMLDivElement) " (div is converted to a string). It is necessary either to use the ID, or to make the element a global variable before the timeout, etc. (this is absolutely heresy).