I have a piece of JavaScript that's going to fire event every second after 10s, it's going to gradually increase the interval time. I'm new to JavaScript so it would be nice to have a codereview.
var _lastHeartbeat = window.performance.now();
var _config = {
heartbeat: {
start: 1000,
max: 10000000,
cap: 10000,
degrade: function(interval) {
var int = Math.pow(interval, 1.15);
return Math.round(int / 10000) * 10000;
}
}
}
var heartbeat = function() {
var end = window.performance.now();
var timespent = Math.round((end - _lastHeartbeat) / 1000) * 1000
console.log('test: ' + timespent); // fire event
}
var _startHeartbeat = function() {
var beat = function(interval) {
setTimeout(function() {
heartbeat();
var end = window.performance.now();
var timespent = Math.round((end - _lastHeartbeat) / 1000) * 1000
if (timespent < _config.heartbeat.cap) {
beat(interval);
} else if (interval < _config.heartbeat.max) {
beat(_config.heartbeat.degrade(timespent));
}
}, interval);
};
beat(_config.heartbeat.start);
}
_startHeartbeat();
Here's JsFiddle. https://jsfiddle.net/noppanit/5cpbyyrg/1/