Skip to content

Instantly share code, notes, and snippets.

@rap0so
Last active February 7, 2021 23:08
Show Gist options
  • Save rap0so/f907a7241b74c61f80e27a2315d296ef to your computer and use it in GitHub Desktop.
Save rap0so/f907a7241b74c61f80e27a2315d296ef to your computer and use it in GitHub Desktop.
import waitForTrue from './waitForTrue'
export default waitForTrue
{
"name": "waitForTrue",
"version": "1.0.0"
}
/**
* @summary Function that wait for var be ready, and when its ready it will run the callback
* @description Function will be reexecuted on setTimeOut until var is ready
* when var exists the `callback` will be executed
* the function usually runs by passing an global param as `variable`
*
* @param { Function } fnIsTrue - The function that should return an boolean
* @param { Function } callback - The callback function to be executed
* @param { Number } time? - Optional time to try again
*/
function waitForTrue (fnIsTrue, callback, time) {
if (fnIsTrue()) {
return callback();
}
setTimeout(function() {
waitForTrue(fnIsTrue, callback, time);
}, time || 50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment