Javascript 原型事件未在另一个函数($.proxy)内触发

Javascript 原型事件未在另一个函数($.proxy)内触发,javascript,jquery,Javascript,Jquery,我一直在尝试修复一个为时间选择器找到的插件,它如下所示: 除一条关键线路外,其工作原理几乎与预期一致: this.tpl.find('.timingfield_hours .timingfield_next') .on('mouseup', function() { clearInterval(timeoutId); return false; }) .on('mousedown', function(e) { timeo

我一直在尝试修复一个为时间选择器找到的插件,它如下所示:

除一条关键线路外,其工作原理几乎与预期一致:

this.tpl.find('.timingfield_hours .timingfield_next')
    .on('mouseup', function() {
        clearInterval(timeoutId);
        return false;
    })
    .on('mousedown', function(e) {
        timeoutId = setInterval($.proxy(this.upHour, this), 100);
        return false;
    });
据我所知,这段代码用于在用户按下按钮后每100毫秒触发一次upHour的方法(相同的代码可以复制几分钟或几秒钟)。但是,它根本不会被触发,但是如果您删除间隔的一部分,然后像这样调用:

this.tpl.find('.timingfield_hours .timingfield_next')
    .on('mousedown', $.proxy(this.upHour, this));
它可以按预期工作,但每次都必须逐个单击才能工作

我确信setInterval正在被触发,因为我将其修改为:

this.tpl.find('.timingfield_hours .timingfield_next')
    .on('mouseup', function() {
        clearInterval(timeoutId);
        return false;
    })
    .on('mousedown', function(e) {
        timeoutId = setInterval(function(){
            console.log('x');
            $.proxy(this.upHour, this)
        }, 100);
        return false;
    });
我知道timeoutId正在移动,但字段一点也没有改变。这是对代码的篡改:


问题的位置在JS的第32行。有人知道我应该改变什么才能让它工作吗?或者为什么不从内部函数调用该函数?谢谢您的帮助。

您需要在
init
函数本身中绑定
this.upHour
。在mousedown处理程序中,此将是一个按钮,而不是插件实例

因为你的方法

timeoutId = setInterval(function(){
        console.log('x');

        // this only creates new function and never call it
        // also `this` will be global or undefined (in strict mode) 
        $.proxy(this.upHour, this)
    }, 100);

您需要在
init
函数本身中绑定
this.upHour
。在mousedown处理程序中,此将是一个按钮,而不是插件实例

因为你的方法

timeoutId = setInterval(function(){
        console.log('x');

        // this only creates new function and never call it
        // also `this` will be global or undefined (in strict mode) 
        $.proxy(this.upHour, this)
    }, 100);

@很高兴我能帮忙。@FedericoNavarrete很高兴我能帮忙。