0

Currently, I am running a function every five seconds.

setInterval(function(){loadLog()},5000);

However, how can I also run the function on demand (on the click of a button) without having to wait for the intervals?

I think that I MAY need to use something like clearInterval, but I'm not sure.

2
  • Are you asking how to run a function on a button click or how to stop the recurring interval? Commented Apr 19, 2014 at 0:43
  • @jfriend00 I'm asking how to run the function instantly (on the click of a button) while the setInterval is still running. Commented Apr 19, 2014 at 0:47

2 Answers 2

1

You can just use a click event handler:

 <button id="runLog">Run Now</button>

Javascript:

 document.getElementById("runLog").addEventListener("click", loadLog);
Sign up to request clarification or add additional context in comments.

Comments

0

Rather than putting your function within the setInterval why don't you just write your function:

function loadLog() {
    //do something
};

and then call the function within the setInterval and also on the click event of your button:

setInterval(loadLog, 5000);
$('#button').click(loadLog);

at least this worked for me

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.