获取对在JavaScript中运行函数的对象的引用

时间:2015-03-16 14:35:15

标签: javascript jquery

我正在尝试使用jQuery扩展来检查服务器是否存活。 这里的扩展名为:https://gist.github.com/jerone/3487795

到目前为止我的代码:

    // Device scanner class
        function deviceScanner(list){
                this.list = list;
                this.totalDevices = list.length;
                this.scan = function () {
                        
                        for(var i = 0; i < this.totalDevices; i++){
                                
                                $.Ping(list[i],1000)
                                        .done( function(success, url, time, on) {
                                            found(url);
                                        })
                                        .fail( function(success, url, time, on) {
                                            notFound(url);
                                        });  
                        }
                }
                
                this.found = function (address){
                        alert(address);       
                }
                this.notFound = function (address){
                        alert(address);       
                }
        }
      
      var scanner = new deviceScanner([KVMRxDefaultIP, KVMTx1DefaultIP, KVMTx2DefaultIP]);
      
      $("#bt1").click(function(){
                scanner.scan();
        });

如何在DeviceScanner中使Ping完成或失败存储结果?

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
    // Device scanner class
        function deviceScanner(list){
                var me = this;  // HERE (1)
                this.list = list;
                this.totalDevices = list.length;
                this.scan = function () {
                        
                        for(var i = 0; i < this.totalDevices; i++){
                                
                                $.Ping(list[i],1000)
                                        .done( function(success, url, time, on) {
                                            me.property_done = true; // HERE (2)
                                            found(url);
                                        })
                                        .fail( function(success, url, time, on) {
                                            me.property_failed = true; // HERE (3)
                                            notFound(url);
                                        });  
                        }
                }
                
                this.found = function (address){
                        alert(address);       
                }
                this.notFound = function (address){
                        alert(address);       
                }
        }
      
      var scanner = new deviceScanner([KVMRxDefaultIP, KVMTx1DefaultIP, KVMTx2DefaultIP]);
      
      $("#bt1").click(function(){
                scanner.scan();
        });
&#13;
&#13;
&#13;