0

was hoping you could help me out.

I'm trying to figure out a way to change javascript code using javascript.

If thats not possible, then if you could suggest another way for me to accomplish what i'm trying to do it would be great.

Basically, a user selects option, which generates a customized javascript code for a clock (using php via ajax techniques). now this code is run using the eval statement, and the clock is displayed in a particular element.

the eval() statement is run each time a response is received from server via AJAX.

Problem is, since it is a clock, I have used setInterval() to constantly update the time. Now if multiple ajax calls are run, multiple functions start to run in a loop.

i.e, after first server call we have this running:

setInterval(function and code for clock with format 1,1000);

AND

setInterval(function and code for clock with format 2,1000);

so basically, I want to somehow stop the first one from running before the second one starts..any ideas?

thanks in advance.

2 Answers 2

1

i think you can assi setIntervel to an variable and clear that while next call

 var a = setInterval(function and code for clock with format 1,1000);

 clearInterval(a);

http://www.w3schools.com/jsref/met_win_clearinterval.asp

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

Comments

0

setInterval returns a numeric identifier of the created interval, which you can pass to clearInterval to stop it. Simply store the identifier in some variable and stop it right before starting the new interval.

var intervalID;

// When creating the first interval
intervalID = setInterval(function() { ... }, 1000);

// When creating the second interval
clearInterval(intervalID);
intervalID = setInterval(function() { ... }, 2000);

If you're unsure if an interval was already started or not, you can first check if intervalID is set and clear it if necessary.

if(intervalID) clearInterval(intervalID);
intervalID = setInterval(function() { ... }, 1000);

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.