Like setInterval, but called a configurable number of times.
// 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)
var rangeInterval = require('range-interval')
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.
opts
is an object of options with the following properties:start
is the starting number. Optional, defaults to0
.step
is the amount the number is incremented on each iteration. (This can be negative.) Optional, defaults to1
.end
is the number at which the loop ends. Required.interval
is the number of ms between each iteration. Required.
each
is a function that is passed the following arguments:number
is a number betweenopts.start
andopts.end
that is incremented byopts.step
each iteration.
cb
is an optional function that is called afteropts.end
is reached. No arguments are passed to it.
Install using npm
npm install range-interval