[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 745
  • Last Modified:

Re-set window.setInterval function with onClick

I have a function that I want to run on a set interval, once there is a click action on a specific target I want to do something and then reset the interval of the function.

window.setInterval(function faded(){
// Keep doing something and then reset me with onclick
}, 3000);

$(".target  li a").click(function(){

if(clickTrack == 0) {
// Do something and then
setInterval("faded()", 1000);

}else if(clickTrack == 1) {
// Do something and then
setInterval("faded()", 1000);
}

Open in new window

0
gvilla23
Asked:
gvilla23
1 Solution
 
Julian HansenCommented:
Save the return from SetInterval, call clearInterval with that ID and then call setInterval again

so

var id = setInterval("doSomething()", 3000);

$(".target  li a").click(function(){
clearInterval(id);
if(clickTrack == 0) {
// Do something and then
id = setInterval("faded()", 1000);

}else if(clickTrack == 1) {
// Do something and then
id=setInterval("faded()", 1000);
}
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now