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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…