GreaseMonkey countdown not working?

后端 未结 2 620
情歌与酒
情歌与酒 2020-12-21 16:33

I\'m very confused, with greasemonkey setTimeout just isn\'t working, it never calls the function, looking online people say greasemonkey doesn\'t support setTimeout, is the

相关标签:
2条回答
  • 2020-12-21 17:02

    The problem lies in the textual parameter of setTimeout. It works very well with greasemonkey but if you use textual commands instead of callbacks, the code is never executed since greasemonkey sandbox is cleared by the time the setTimeout fires. It tries to run eval with the textual parameter wchis in turn tries to call function countdown which doesn't exist by that time anymore.

    Currently the program flow is as follows:

    1. function countdown(){}
    
    2. setTimeout("countdown()", 1000);
    
    3. clearGreasemonkeySandbox();
    
    4. ... wait 1 sec...
    
    5. eval("countdown()"); // <- countdown doesn't exist anymore
    

    So you should use callbacks instead, this way a pointer to a function is used instead of the full sentence.

    setTimeout(function(){
        countdown(--time, id);
    }, 1000);
    
    0 讨论(0)
  • 2020-12-21 17:09

    In the end I ended up using

    window.setTimeout(bla, 1000);
    

    and

    window.bla = function() { alert("cool"); }

    0 讨论(0)
提交回复
热议问题