Greasemonkey setTimeout和setInterval未触发

Greasemonkey setTimeout and setInterval not triggering

本文关键字:setInterval setTimeout Greasemonkey      更新时间:2023-09-26

我正在使用以下代码,我得到的唯一警报是alert(t)。我已经尝试过unsafeWindow.setTimeoutwindow.setTimeout和其他变体,包括setInterval,但都不起作用。

$('#on').change(function () { t = setTimeout(function () { starttimer() },1000);timer_on = true;alert(t);  });
    function starttimer() { 
    alert('triggered');
        if (timer_on) {
            alert('timerstarted');
            t = setTimeout(function () { startimer() },1000);
            }
    }

编辑:也没有错误。脚本继续执行,t具有正常值,只是函数从未运行。

不要那样写代码!学会爱上jsBeautifier和JSLint。

你在用什么版本的FF和Greasemonkey?一些组合在计时器和/或事件侦听器中出现警报问题。

无论如何,$('#time').val ()可能不存在,或者不是你想象的那样。$('#time')指的是<input>吗?

试试这个代码:

// t and timer_on are global variables.
$('#on').change ( function () {
    var timeVal = $('#time').val ()  ||  1; //-- Account for empty and NaN
    timeVal     = parseInt (timeVal, 10) * 1000;
    alert ('Time val = ' + timeVal);
    t           = setTimeout (starttimer, timeVal);
    timer_on    = true;
    alert (t);
} );
function starttimer () {
    alert ('triggered');
    if (timer_on) {
        alert ('timerstarted');
        var timeVal = $('#time').val ()  ||  1; 
        timeVal     = parseInt (timeVal, 10) * 1000;
        alert ('Time val = ' + timeVal);
        t           = setTimeout (starttimer, timeVal);
    }
}