Created
April 2, 2019 14:22
-
-
Save sketchpunk/da501c57d190ede3894fffbecb00040b to your computer and use it in GitHub Desktop.
Run function on interval till it returns true
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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