WaitFor

一直等待并执行回调函数

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/464516/1179152/WaitFor.js

  1. // ==UserScript==
  2. // @name WaitFor
  3. // @namespace http://bbs.91wc.net/?wait-for
  4. // @version 0.1
  5. // @description 一直等待并执行回调函数
  6. // @author Wilson
  7. // ==/UserScript==
  8.  
  9. function WaitFor(cond, callback, delay) {
  10. delay = delay || 100;
  11. var timer = setTimeout(function(){
  12. if(timer) clearTimeout(timer);
  13. if(cond && cond()) callback();
  14. WaitFor(cond, callback, delay);
  15. }, delay);
  16. }
  17.  
  18. //使用:
  19. //WaitFor(()=>{return true}, ()=>{console.log(1)});
  20. //cond 条件回调函数,当cond()为真则执行callback
  21. //callback 回调函数
  22. //delay 多久检查一次条件,单位ms,默认100