对象上的javascript setInterval';s函数只调用2x

对象上的javascript setInterval';s函数只调用2x,javascript,Javascript,我有这个密码。但由于某些原因,即使连续移动鼠标,它也只能发出两次警报。我一直在这方面工作,并在firebug中进行调查,当我移动鼠标时,focus是真的。我一直在想到底发生了什么。。即使我这样做: var focus = true; function z() { this.t = 0; this.p = function (t) { if (focus == true) { this.t = t; alert(th

我有这个密码。但由于某些原因,即使连续移动鼠标,它也只能发出两次警报。我一直在这方面工作,并在firebug中进行调查,当我移动鼠标时,focus是真的。我一直在想到底发生了什么。。即使我这样做:

var focus = true;

function z() {
    this.t = 0;
    this.p = function (t) {
        if (focus == true) {
            this.t = t;
            alert(this.t);

        }

    }
}
var zp = new z();
setTimeout('zp.p(0)', 100);
window.setInterval('zp.p(1)', 2000);
var ftimer = setTimeout('focus=false', 2000);

document.addEventListener('mousemove', function (e) {
    clearTimeout(ftimer);
    focus = true;
    ftimer = setTimeout('focus=false', 2000);
}, false);
它仍然只发出两次警报


我也尝试过使用循环setTimeout函数,但也不起作用。它快把我逼疯了

我发现它只是与firefox相关,firefox被一行代码阻塞了,所以它无法运行。所以我修好了,现在一切都好了

很高兴你找到了bug

我会使用更多的函数作为第一类对象编写代码,而使用更少的
eval
逻辑:

function z() {
    this.t = 0;
    this.p = function (t) {
        if (focus == true) {
            this.t = t;

        }
        alert(this.t);

    }
}

我在第10行使用了一个函数:
(函数(obj){return function(){…})(this),和。

它似乎一直在提醒我。你到底想做什么?它总是提醒我
1
,似乎没有任何特殊用途。在
setTimeout
中使用字符串也会使用
eval
,这是一种邪恶的行为,在这种情况下使用匿名函数。您应该告诉我们这会产生什么样的预期效果,或者重构代码以表明这一点。因为谁知道a
z
是什么。你的代码肯定是次优的,如果你说出你真正想做的,人们可以(也可能会)想出一个更好的版本。谢谢!我真的很感激。
var focus = true;
function z() {
    var that = this; // we won't always have the correct value of "this"
    this.focus = true;
    this.t = 0;
    this.p = function (t) {
        if (that.focus == true) {
            that.t = t;
            alert(t);
        }
    }
    this.fade = ( function(obj){ return function(){ obj.focus = false; } } )(this); // Using self-invokation
}
var zp = new z();
setTimeout(zp.p, 100, 0);
window.setInterval(zp.p, 2000, 1);
var ftimer = setTimeout(zp.fade, 2000);
document.addEventListener('mousemove', function (e) {
    clearTimeout(ftimer);
    zp.focus = true;
    ftimer = setTimeout(zp.fade, 2000);
}, false);