简体   繁体   English

为什么我的clearInterval不起作用?

[英]why my clearInterval doesn't work?

Using the node.js,I set a timer to connect the TCP server.when it success ,it will be cleared.However it doesn't work.just like this: 使用node.js,我设置了一个定时器来连接TCP服务器。当它成功时,它将被清除。但是它不起作用。就像这样:

var net = require('net');
var connectTransferTick=null;

var client ;
client = new net.Socket();
function clientConnect() {
    client.connect(8009, '127.0.0.1', function () {
        console.log('success,clear timer');
        if(connectTransferTick!=null);
        {
            clearInterval(connectTransferTick);
        }
    });
    client.on('error',function()
    {});
    client.on('close', function () {
        console.log('The server had been closed,Try to connect it again later');
        if(connectTransferTick!=null)
        {
            console.log(clearInterval(connectTransferTick));
        }
        connectTransferTick = setInterval(clientConnect,1000);
    });
}
clientConnect();

Run it ,you will find the timer cann't clear. 运行它,你会发现计时器无法清除。

while,I Just put this code in the function clientConnect(),like this: 虽然,我只是将此代码放在函数clientConnect()中,如下所示:

function clientConnect() {
    client = new net.Socket();

The timer could be cleared.I can't explaint it.why ? 计时器可以清除。我无法解释它。为什么? please help me.Thanks a lot. 请帮帮我。谢谢。

I think because setInterval is called when the Socket connection is closed, but you're attempting to cancel the interval earlier than this, when the Socket is first opened. 我认为因为在Socket连接关闭时调用了setInterval,但是当你第一次打开Socket时,你试图在此之前取消这个时间间隔。

Thus, when you call 因此,当你打电话

clearInterval(connectTransferTick)

at this point connectTransferTick is still null , because 此时connectTransferTick仍然为null ,因为

connectTransferTick = setInterval()

has not yet happened. 还没有发生。

Because when the close is met, it will cause the clientConnect to be call at least again, and register another onclose to client. 因为当满足close时,它将导致至少再次调用clientConnect ,并将另一个onclose注册到客户端。 So every time the close is met, you register more setInterval while you can only clear the latest. 因此,每次满足结束时,您只能清除最新的setInterval

So 所以

function clientConnect() {
    client = new net.Socket();

works because you create a new socket, and it haven't defined the onclose handler yet, so it won't execute setInterval more than once. 因为你创建了一个新的套接字,它还没有定义onclose处理程序,所以它不会多次执行setInterval

So you should use a flag to prevent register the events many times: 所以你应该使用一个标志来防止多次注册事件:

var net = require('net');
var connectTransferTick=null;

var client ;
client = new net.Socket();
function clientConnect(isInited) {
    client.connect(8009, '127.0.0.1', function () {
        console.log('success,clear timer');
        if(connectTransferTick!=null);
        {
            clearInterval(connectTransferTick);
        }
    });

    // Prevent events registered many times.
    if (!isInited) {
        client.on('error',function(){});
        client.on('close', function () {
            console.log('The server had been closed,Try to connect it again later');
            if(connectTransferTick!=null)
            {
                console.log(clearInterval(connectTransferTick));
            }
            // Pass a flag to the clientConnect
            connectTransferTick = setInterval(clientConnect,1000, true);
        });    
    }

}
clientConnect();

In addition, I think setTimeout should be enough to handle this? 另外,我认为setTimeout应该足以处理这个?

var net = require('net');

var client ;
client = new net.Socket();
function clientConnect(isInited) {
    client.connect(8009, '127.0.0.1', function () {
        console.log('success,clear timer');
    });

    // Prevent events registered many times.
    if (!isInited) {
        client.on('error',function(){});
        client.on('close', function () {
            console.log('The server had been closed,Try to connect it again later');
            connectTransferTick = setTimeout(clientConnect,1000, true);
        });    
    }

}
clientConnect();

Some information/advice on the use of interval identifiers: 关于使用区间标识符的一些信息/建议:

Node.js: Node.js的:

The return value of setInterval() is an object, which contains all of the information about the interval you created. setInterval()的返回值是一个对象,它包含有关您创建的时间间隔的所有信息。 Even after calling clearInterval() with the object as an parameter, the object is not set to null. 即使在将对象作为参数调用clearInterval()之后,该对象也不会设置为null。 You should check the object's _repeat flag to check whether the interval is running or not. 您应该检查对象的_repeat标志以检查间隔是否正在运行。

var interval_id = setInterval(function() {  }, 10000);

if (interval_id._repeat)
{
    clearInterval(interval_id);
}

Client-side javascript: 客户端javascript:

The return value of setInterval() is an int, which is the id of the interval you created. setInterval()的返回值是一个int,它是您创建的时间间隔的id。 When you call clearInterval() , it doesn't unset the variable that stores this integer id. 当您调用clearInterval() ,它不会取消设置存储此整数id的变量。 You should do this manually after calling clearInterval() if you are going to use it as a flag for whether the interval is running or not. 您应该在调用clearInterval()之后手动执行此操作,如果您要将其用作间隔是否正在运行的标志。

var interval_id = setInterval(function() {  }, 10000);

if (interval_id)
{
    clearInterval(interval_id);
    interval_id = null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM