admin管理员组

文章数量:1134549

How do I reset a setInterval timer back to 0?

var myTimer = setInterval(function() {
  console.log('idle');
}, 4000);

I tried clearInterval(myTimer) but that completely stops the interval. I want it to restart from 0.

How do I reset a setInterval timer back to 0?

var myTimer = setInterval(function() {
  console.log('idle');
}, 4000);

I tried clearInterval(myTimer) but that completely stops the interval. I want it to restart from 0.

Share Improve this question edited Jul 28, 2018 at 21:13 Ivar 6,77912 gold badges56 silver badges67 bronze badges asked Nov 14, 2011 at 18:42 Rik de VosRik de Vos 3,5176 gold badges30 silver badges35 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 230

If by "restart", you mean to start a new 4 second interval at this moment, then you must stop and restart the timer.

function myFn() {console.log('idle');}

var myTimer = setInterval(myFn, 4000);

// Then, later at some future time, 
// to restart a new 4 second interval starting at this exact moment in time
clearInterval(myTimer);
myTimer = setInterval(myFn, 4000);

You could also use a little timer object that offers a reset feature:

function Timer(fn, t) {
    var timerObj = setInterval(fn, t);

    this.stop = function() {
        if (timerObj) {
            clearInterval(timerObj);
            timerObj = null;
        }
        return this;
    }

    // start timer using current settings (if it's not already running)
    this.start = function() {
        if (!timerObj) {
            this.stop();
            timerObj = setInterval(fn, t);
        }
        return this;
    }

    // start with new or original interval, stop current interval
    this.reset = function(newT = t) {
        t = newT;
        return this.stop().start();
    }
}

Usage:

var timer = new Timer(function() {
    // your function here
}, 5000);


// switch interval to 10 seconds
timer.reset(10000);

// stop the timer
timer.stop();

// start the timer
timer.start();

Working demo: https://jsfiddle.net/jfriend00/t17vz506/

Once you clear the interval using clearInterval you could setInterval once again. And to avoid repeating the callback externalize it as a separate function:

var ticker = function() {
    console.log('idle');
};

then:

var myTimer = window.setInterval(ticker, 4000);

then when you decide to restart:

window.clearInterval(myTimer);
myTimer = window.setInterval(ticker, 4000);

Here's Typescript and Nuxt 3 version if anyone's interested :]

Composable useInterval.ts

useInterval.ts

export function useInterval (callback: CallableFunction, interval: number): Interval { // Argument interval = milliseconds
  return new Interval(callback, interval)
}

class Interval {
  private timer = null

  constructor (private callback, private interval) {
  }

  start () {
    this.timer = setInterval(this.callback, this.interval)
  }

  stop () {
    clearInterval(this.timer)
    this.timer = null
  }

  restart (interval = 0) {
    this.stop()

    if (interval) {
      this.interval = interval
    }

    this.start()
  }
}

Example usage

const interval = useInterval(function() {
// your function here
}, 5000);    

// Reset the interval and set it to 10s
interval.reset(10000);

// Stop the interval
interval.stop();

// Start the interval
interval.start();

本文标签: javascriptHow do I reset the setInterval timerStack Overflow