Skip to content

Instantly share code, notes, and snippets.

@Solomko2
Created January 11, 2019 09:42
Show Gist options
  • Save Solomko2/6880b4634a26bf54aa1dd2873ac9e4f7 to your computer and use it in GitHub Desktop.
Save Solomko2/6880b4634a26bf54aa1dd2873ac9e4f7 to your computer and use it in GitHub Desktop.
timer.js
function Timer(callback, interval){
let timerId;
function executeAndStartTimer(){
callback().then(function makeNewCall(){
timerId = setTimeout(executeAndStartTimer, interval);
});
}
function stop(){
if(timerId){
clearTimeout(timerId);
timerId = 0;
}
}
function start(){
if(!timerId){
executeAndStartTimer();
}
}
return Object.freeze({
start,
stop
});
}
const timer = Timer(getTodos, 2000);
timer.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment