Why is my clearInterval not working?

Using node.js, I set a timer to connect a TCP server. When it is successful, it will be cleansed. However, it does not work. Just like this:

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 that the timer cannot clear.

while, I just put this code in the clientConnect () function, for example:

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

The timer can be cleared. I can’t explain it. please help me. Thank you so much.

+4
source share
3 answers

I think because setInterval is called when the Socket connection is closed, but you are trying to cancel the interval earlier than this, when Socket is opened first.

That way when you call

clearInterval(connectTransferTick)

at this point connectTransferTickstill nullbecause

connectTransferTick = setInterval()

not yet happened.

0
source

, close, , clientConnect , , , onclose . , , setInterval, .

So

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

, onclose, setInterval .

, :

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();

, , 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();
0

/ :

Node.js

setInterval() - , . clearInterval() null. _repeat, , .

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

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

javascript:

The return value setInterval()is an int, which is the identifier of the interval you created. When you call clearInterval(), it does not cancel the variable that stores this integer identifier. You must do this manually after the call clearInterval(), if you intend to use it as a flag for whether the interval works or not.

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

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

All Articles