[英]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. 使用此代码,我试图检查用户是否尝试在3秒的间隔内发送4条以上的消息。 I have the
global.count
variable, and each time a message is sent, I increment it by one. 我有
global.count
变量,每次发送消息时,我都会将其递增1。 Every 3 seconds my setInterval
function makes the value of count
0, so it resets. 我的
setInterval
函数每隔3秒就会使count
的值count
0,因此它将重置。 Before the message is sent I check if the count is 4. If it is, then I fire the freezeEvent
. 在发送消息之前,我要检查计数是否为4。如果是,则触发
freezeEvent
。 In my front-end I have the following code, which disables the input for 5 seconds if the freezeEvent
is fired: 在我的前端,我有以下代码,如果
freezeEvent
被触发,它将禁用输入5秒钟:
$(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. 第一次运行代码时,输入4条消息后,我的输入将被禁用,但
count
永远不会重置为0。启用输入后, count
完全混乱。 Here's what I got after first time when I console logged the count
: 这是我第一次在控制台记录
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 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.
它始终从0到4。但是仍然存在的问题是,它每隔3秒仍不会重置。 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 如果global.count小于4,则删除时间间隔,因此不适用于前[0-3]个值
Another one thing: your global.count is one for each connection? 另一件事:您的global.count是每个连接一个吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.