0

I wrote this function to fly DIV from top right to bottom left:

var myObj;

function infly() {
    myObj=document.getElementById('mydiv');
    myObj.style.right='0px';
    myObj.style.top='0px';
}

function flyer() {  
    var x=parseInt(myObj.style.right);
    var y=parseInt(myObj.style.top); 

    x+=1;
    y+=1;  

    myObj.style.right=x+'px';
    myObj.style.top=y+'px'; 

}

function repeat()
{
setTimeout(flyer,5000)
}

And HTML code is:

<body onLoad="infly()">

<div id="mydiv">
</div>

<a href="" onClick="javascript:repeat()">Fly</a>

...
..
.

But repeat function not work. When I remove this function in every click my DIV fly correctly.

I try with setInterval('fly();', 10); but no success.

Thanks for any helps.


Update:

I edit the code and correct repeat function but still not work.

7 Answers 7

3

use

setTimeout(fly,5000)

instead of

setTimeout("fly()","5000()")
Sign up to request clarification or add additional context in comments.

Comments

2

The second parameter of setTimeout should be a number:

setTimeout(fly,5000)

https://developer.mozilla.org/en/DOM/window.setTimeout

Updated, better not to use "fly()":

code in the alternate syntax, is a string of code you want to execute after delay milliseconds. (Using this syntax is not recommended for the same reasons as using eval())

3 Comments

@WillemMulder That's another optimization. This answer offers an answer to the question, and includes a link to the relevant documentation. So +1.
Never, never, never pass a string to a setTimeout/setInterval or the context will need to be EVALuated. it's definitely a bad practice
@RobW I did not disagree with mamoo's answer per se, but meant to improve on it. eval is evil.
1

Here is code that makes the div fly. I styled it so you could see it. Your code only called flyer once, and without setting the position to absolute the div will not redisplay.

<script type="text/javascript">
  var K_CYCLES = 20;
  var numcycles = 0;

  function infly() {
    myObj = document.getElementById('mydiv');
    myObj.style.right = '0px';
    myObj.style.top = '0px';

  }

  function flyer() {  

    var x = parseInt(myObj.style.right);
    var y = parseInt(myObj.style.top); 

    x += 10;
    y += 10;  

    myObj.style.right = x + 'px';
    myObj.style.top = y + 'px';
    myObj.style.position = 'absolute';

    numcycles++;

    if (numcycles <= K_CYCLES)
    {
      setTimeout("flyer()",1000);
    }
  }

  function repeat()
  {
    flyer();
  }
</script>
</head>
<body onLoad="infly()">

<div id="mydiv" style="width:50px; height: 50px; border: 1px solid red;">
</div>

<input type="button" onClick="repeat()" value="fly"/>
</body>

1 Comment

Good work. But minor issue. Inside repeat function numcycles should be set to 0 so that the cycle repeats.
0

Change your repeat function to look like this

function repeat() { setTimeout("fly()",5000) } 

Comments

0

And if you want the function to constantly move without repeating the click you would need to call repeat(); at the end of fly() as well. (Of corse if that is, what you want to accomplish...)

Comments

0
<a href="javascript:void()" onclick="javascript:repeat()">Fly</a>

Comments

-2
<a href="javascript:void()" onclick="javascript:repeat()">Fly</a>
setTimeout("fly()",5000)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.