range-interval
v2.0.1
Published
Like setInterval, but called a configurable number of times
Downloads
18
Readme
range-interval
Like setInterval, but called a configurable number of times.
demo
examples
// example/example1.js
var rangeInterval = require('range-interval')
var text = 'hello'
var opts = {
start: 0,
step: 1,
end: text.length-1,
interval: 200
}
rangeInterval(opts, function (index) {
console.log(text[index])
})
// h (printed after 200ms)
// e (printed after 400ms)
// l (printed after 600ms)
// l (printed after 800ms)
// o (printed after 1000ms)// example/example2.js
var rangeInterval = require('range-interval')
var text = '0123456789abcdef'
var str = ''
rangeInterval({
start: 2,
step: 3,
end: text.length-1,
interval: 200
}, function (index) {
str += text[index]
}, function () {
console.log(str)
})
// 258be (printed after 1000ms)api
var rangeInterval = require('range-interval')rangeInterval(opts, each[, cb])
The each function is called every opts.interval milliseconds. It is passed a number that starts at opts.start, and steps opts.step closer each iteration, until it gets to opts.end. Then the cb is called.
optsis an object of options with the following properties:startis the starting number. Optional, defaults to0.stepis the amount the number is incremented on each iteration. (This can be negative.) Optional, defaults to1.endis the number at which the loop ends. Required.intervalis the number of ms between each iteration. Required.
eachis a function that is passed the following arguments:numberis a number betweenopts.startandopts.endthat is incremented byopts.stepeach iteration.
cbis an optional function that is called afteropts.endis reached. No arguments are passed to it.
install
Install using npm
npm install range-interval