Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. /**
  2. * Waits for a condition specified by 'conditionF' to be 'true' and calls the 'callbackF' function afterwards.
  3. *
  4. * @param conditionF function called to check the condition
  5. * @param callbackF function called when the condition was met, or when the 'maxTime' expired
  6. * @param interval (optional) interval to check
  7. * @param maxTime (optional) maximum time to expire
  8. */
  9. function waitForCondition(conditionF, callbackF, interval, maxTime) {
  10. if (!interval) interval = 500;
  11. var startTime = Date.now();
  12. var timer = setInterval(function () {
  13. if (conditionF()) {
  14. clearInterval(timer);
  15. callbackF(true);
  16. }
  17.  
  18. if (maxTime) {
  19. if (Date.now() - startTime > maxTime) {
  20. callbackF(false);
  21. }
  22. }
  23. }, interval)
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement