Javascript this 对象在间隔/超时内

发布于 2024-10-31 10:21:43 字数 1096 浏览 0 评论 0原文

我有一个方法,它是一个很大的 setInterval 语句,它需要从间隔内部访问拥有该方法的对象的 this 对象。我实现了一个简单的闭包,但它看起来不太优雅:

connect: function(to, rate, callback){
    var cthis = this,                             //set cthis to this,
    connectIntervalID = setInterval(function(){
        if(cthis.attemptConnect(to)){             //reference it here,
            clearInterval(connectIntervalID)
            cthis.startListening(10)              //here,
            callback && callback.apply(cthis, []) //and here
        }
    }, rate)
}

如果您想使用thisapplycall来实现em> 而不是 cthis

connect: function(to, rate, callback){
    var cthis = this,
    tempFunc = function(){
        if(this.attemptConnect(to)){                 
            clearInterval(connectIntervalID)
            this.startListening(10)              
            callback && callback.apply(this, []) 
         }
     }�       
     connectIntervalID = setInterval(function(){tempFunc.apply(cthis, [])}, rate)
 }

然而,这似乎更糟糕......

I have a method that is a big setInterval statement, and it needs access to the this object of the object that owns the method from inside the interval. I implemented a simple closure, but it doesn't seem very elegant:

connect: function(to, rate, callback){
    var cthis = this,                             //set cthis to this,
    connectIntervalID = setInterval(function(){
        if(cthis.attemptConnect(to)){             //reference it here,
            clearInterval(connectIntervalID)
            cthis.startListening(10)              //here,
            callback && callback.apply(cthis, []) //and here
        }
    }, rate)
}

You could also do it with apply or call, if you wanted to use this instead of cthis

connect: function(to, rate, callback){
    var cthis = this,
    tempFunc = function(){
        if(this.attemptConnect(to)){                 
            clearInterval(connectIntervalID)
            this.startListening(10)              
            callback && callback.apply(this, []) 
         }
     }�       
     connectIntervalID = setInterval(function(){tempFunc.apply(cthis, [])}, rate)
 }

However, that seems even worse...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

誰認得朕 2024-11-07 10:21:43

使用 .bind 会让它更好一点(在我看来,你可能同意也可能不同意):

支持代码:

function $A(args){
   var out = [];
   for(var i=0, l=args.length; i<l; i++){ out.push(args[i]); }
   return out;
}

Function.prototype.bind = function() {
   var __method = this, args = $A(arguments), object = args.shift();

   return function() {
      return __method.apply(object || this, args.concat( $A(arguments) ));
   };
};

并且你的代码变成:

connect: function(to, rate, callback){
    connectIntervalID = setInterval((function(){
        if(this.attemptConnect(to)){             //reference it here,
            clearInterval(connectIntervalID)
            this.startListening(10)              //here,
            callback && callback.apply(this, []) //and here
        }
    }).bind(this), rate)
}

但我担心你不会得到很多更好的。

Using a .bind makes it a bit better (in my opinion, you may or may not agree):

support code:

function $A(args){
   var out = [];
   for(var i=0, l=args.length; i<l; i++){ out.push(args[i]); }
   return out;
}

Function.prototype.bind = function() {
   var __method = this, args = $A(arguments), object = args.shift();

   return function() {
      return __method.apply(object || this, args.concat( $A(arguments) ));
   };
};

and your code becomes:

connect: function(to, rate, callback){
    connectIntervalID = setInterval((function(){
        if(this.attemptConnect(to)){             //reference it here,
            clearInterval(connectIntervalID)
            this.startListening(10)              //here,
            callback && callback.apply(this, []) //and here
        }
    }).bind(this), rate)
}

But I'm afraid you won't get a whole lot better.

生生漫 2024-11-07 10:21:43

您的第一个示例或多或少是执行此操作的标准方法。我唯一的建议是将你的变量命名为 cthis 以外的其他名称;使其描述被绑定的对象。

Javascript 1.8.5 添加了 Function.prototype.bind 以不同的方式解决这个问题,但这对大多数人来说并不是一个有用的解决方案。

Your first example is more-or-less the standard way to do this. My only suggestion would be to call your variable something other than cthis; make it descriptive of the object being bound.

Javascript 1.8.5 adds Function.prototype.bind to solve this problem in a different way, but that's not a useful solution for most people.

╰つ倒转 2024-11-07 10:21:43

我会将 setInterval 函数分解为它自己的函数,并附加到与 connect 相同的对象。这样,就可以清楚 this 引用同一个对象:

connect: function (to, rate, callback) {
    var obj = this;
    var intervalId = setInterval(function () {
        obj.connectInterval(intervalId, callback);
    }, rate);
},
connectInterval: function (intervalId, callback) {
    if (this.attemptConnect(to)) {
        clearInterval(intervalId);
        this.startListening(10);
        callback && callback.apply(this, []);
    }
}

I'd break out the setInterval function into its own function, attached to the same object as connect. In this way, it will be clear that this refers to the same object:

connect: function (to, rate, callback) {
    var obj = this;
    var intervalId = setInterval(function () {
        obj.connectInterval(intervalId, callback);
    }, rate);
},
connectInterval: function (intervalId, callback) {
    if (this.attemptConnect(to)) {
        clearInterval(intervalId);
        this.startListening(10);
        callback && callback.apply(this, []);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文