var heartCheck = { //心跳检测 timeout: 3000,socket:websocket.getSocket() timeoutObj: null, serverTimeoutObj: null, start: function(ws){ console.log('start'); var self = this; this.timeoutObj && clearTimeout(this.timeoutObj); this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj); this.timeoutObj = setTimeout(function(){ ws.send("123456789"); self.serverTimeoutObj = setTimeout(function() { ws.close(); }, self.timeout); }, this.timeout) } }