Skip to content

Instantly share code, notes, and snippets.

@sketchpunk
Created April 2, 2019 14:22
Show Gist options
  • Save sketchpunk/da501c57d190ede3894fffbecb00040b to your computer and use it in GitHub Desktop.
Save sketchpunk/da501c57d190ede3894fffbecb00040b to your computer and use it in GitHub Desktop.
Run function on interval till it returns true
class RunTillTrue{
constructor( s, func = null ){
this.sec = s;
this.thread = null;
this.task = func;
this.active = false;
this.run = ()=>{
if( !this.active || this.task() == true ) this.stop();
};
}
stop(){
this.active = false;
clearInterval( this.thread );
this.thread = null;
}
start(){
if( !this.thread ) clearInterval( this.thread );
this.active = true;
this.thread = setInterval( this.run, this.sec );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment