简体   繁体   中英

setInterval doesn't work correctly after first time Node

I have the following code:

var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
global.count =  0;

io.on('connection', function(socket){
    console.log('CONNECTED');

    socket.on('message', function(msg){

        var intVal = setInterval(function(){
            global.count = 0;
        },3000);

        console.log(global.count);

        if(global.count == 4){
            io.emit('freezeEvent');
            global.count = 0;
        }
        else{
            global.count++;
            io.emit('message',msg);
            clearInterval(intVal);
        }
    });

    socket.on('disconnect', function () {
        console.log('DISCONNECTED');
    });
});

server.listen(3000);


With this code I'm trying to check if the user tries to send more than 4 messages in an interval of 3 seconds. I have the global.count variable, and each time a message is sent, I increment it by one. Every 3 seconds my setInterval function makes the value of count 0, so it resets. Before the message is sent I check if the count is 4. If it is, then I fire the freezeEvent . In my front-end I have the following code, which disables the input for 5 seconds if the freezeEvent is fired:

$(function () {

    var socket = io('socket.loc:3000');

    socket.on('connect', function () {

        socket.on('freezeEvent', function(){

            $('.inp').attr("disabled", true);

            setTimeout(function(){ 
                $('.inp').attr("disabled",false);               
            }, 5000);
        });

    });
});

First time I run the code, my input becomes disabled after 4 messages, but the count never resets to 0. After the input becomes enabled, the count completely messes up. Here's what I got after first time when I console logged the count :

4
0
0
1
2
3
4
0
0
1
0
1
2
3
4

Can you please tell me what causes this and how can it be fixed?

UPDATE

I have changed my back-end code to the following and the counter doesn't mess up anymore. It always goes from 0 to 4. But the problem that remains is that it still doesn't reset after every 3 seconds. Here's my code:

var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
global.count =  0;

global.intVal = setInterval(function(){
    global.count = 0;
},3000);

io.on('connection', function(socket){
    console.log('CONNECTED');

    socket.on('message', function(msg){

        console.log(global.count);

        if(global.count == 4){
            io.emit('freezeEvent');
            global.count = 0;
        }
        else{
            global.count++;
            io.emit('message',msg);
            clearInterval(global.intVal);
        }
    });

    socket.on('disconnect', function () {
        console.log('DISCONNECTED');
    });
});

server.listen(3000);

I suspect that the problem is

if(global.count == 4){
    io.emit('freezeEvent');
    global.count = 0;
}
else{
    global.count++;
    io.emit('message',msg);
    clearInterval(intVal);
}

you remove interval if global.count less than 4 so it didn't work for first [0-3] values

Another one thing: your global.count is one for each connection?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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