Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

setinterval-plus

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

setinterval-plus

A replacment to normal setInterval which includes pausing and resuming

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

setinterval-plus

Build Status Coverage Status npm version

js-standard-style

setinterval-plus is inspired by the stackoverflow answer on how to pause and resume normal setInterval timers. What setinterval-plus provides is a smiliar API as the vanila function setIntreval only the object returned isn't a Timer but instead it returns and object with the ability to start/pause/resume/stop the Timer object with a very intuitive and foolproof API.

Installation

$ npm i setinterval-plus

Create a new setinterval-plus

After creating a new instance the timer will start running.

Params:

  • callback - The function to be called on each setInterval iteration. Can't take any args since the function will be called like this: callback()
  • interval - Time in ms to wait between intervals
Example:
var callback = function () {
  console.log('Run')
}
var timer = new _setInterval(callback, 1000) // Timer starts ticking

API

timer.pause() // Pauses the interval timer
timer.resume() // Resumes the interval timer at the point where the timer was paused (+/- a few ms)
timer.stop() // Stop a timer and clears the interval
timer.start() // Start back a stoped interval with the original interval time
Example:
var _setInterval = require('setinterval-plus')

var runs = 0
var timer = new _setInterval(function () {
  console.log('Run')
  runs++
}, 100)
// console.log(timer)
setTimeout(function () {
  timer.pause()
  setTimeout(function () {
    console.log('Remaining time for Interval: ', timer.remaining) // Will print around 50
    timer.resume()
    setTimeout(function () {
      timer.stop()
      setTimeout(function () {
        timer.start()
        console.log('Remaining time for Interval: ', timer.remaining) // Will print -1 which means it restarted running
        var check = new Date()
        setTimeout(function () {
          console.log('Time before rerunning', new Date() - check)
          console.log(runs) // Should print 15
        }, 1045) // Will run 10 more times
      }, 5) // Wll Stop for 5 ms
    }, 200) // Will run for 200 ms which are 2 full runs (we have 50 ms from before)
  }, 100) // Will pause for 100 ms
}, 350) // Will run for 350 ms which are 3 full runs

Development

$ git clone git@github.com:thehobbit85/setinterval-plus.git
$ cd setinterval-plus
$ npm i
$ npm test
$ npm run coverage

License

MIT

Keywords

FAQs

Package last updated on 02 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc