3

I have this code

<script type="text/javascript"> function test(counter) { if(counter==4) { counter=0; } if(counter==0) { //various stuff } counter = counter + 1; setTimeout(function () { test(counter); }, 7000); } $(document).ready(function () { test(0); }); </script> 

So when the page loads the test(0) function is running.

But i have this link <a onclick="test(1)" rel="2" href="#"> that calls the same function again, having as a result to have the same function running twice.

Is there a way to stop the function that is already running and then start the new one??

2 Answers 2

4

Save the result of setTimeout(), and use it with clearTimeout().

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks with the reply! But how this would help me?
@mathew: Where you have the setTimeout() call, that function returns an identifier for the timer that is setup by it (timerID = setTimeout(...);). In order to cancel a pending timer event, you have to clear it, using clearTimeout(timerID);.
0
function test(counter) { if(counter==4) { counter=0; } if(counter==0) { //various stuff } counter = counter + 1; var quit; //decide whether to continue if(!quit)setTimeout(function () { test(counter); }, 7000); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.