Javascript 在调用事件时每隔几秒钟运行一次代码
我正在创建一个聊天脚本,在有人打字时,我需要每三秒运行一次代码。我可以很容易地使用setInterval每三秒钟运行一次代码,但是将setInterval放在我的按键事件中会导致它在每次按键时都从一开始就开始运行,所以它永远不会运行,直到他们停止键入为止。另一个问题是当他们停止打字时停止它。有什么想法吗Javascript 在调用事件时每隔几秒钟运行一次代码,javascript,jquery,Javascript,Jquery,我正在创建一个聊天脚本,在有人打字时,我需要每三秒运行一次代码。我可以很容易地使用setInterval每三秒钟运行一次代码,但是将setInterval放在我的按键事件中会导致它在每次按键时都从一开始就开始运行,所以它永远不会运行,直到他们停止键入为止。另一个问题是当他们停止打字时停止它。有什么想法吗 $("#message").keypress(function(event){ // Do stuff here every keypress // Every three s
$("#message").keypress(function(event){
// Do stuff here every keypress
// Every three seconds, while someone is typing do this:
ws.send(JSON.stringify({"type": "typing", "from": {"id": client.self.id, "hash": client.self.hash}, "to": {"id": client.partner.id, "hash": client.partner.hash}}));
});
好的,听起来很酷
那么这个,
- 你应该为你的时间间隔取一个变量
- 在按键时,检查该间隔是否存在,如果存在,则清除该间隔
- 然后在同一变量上设置间隔,以便在下一次按键时再次检查间隔是否存在,并可以重复这些步骤
$(function(){
var request;
$("#message").keypress(function (event) {
// First clear the interval if exists
if(request){
clearInterval(request);
}
// the set the interval in the same variable
request = setInterval(function(){
ws.send(JSON.stringify({"type": "typing", "from": {"id": client.self.id, "hash": client.self.hash}, "to": {"id": client.partner.id, "hash": client.partner.hash}}));
}, 3000);
});
});
祝你好运!!祝你快乐 好的,听起来很酷
那么这个,
- 你应该为你的时间间隔取一个变量
- 在按键时,检查该间隔是否存在,如果存在,则清除该间隔
- 然后在同一变量上设置间隔,以便在下一次按键时再次检查间隔是否存在,并可以重复这些步骤
$(function(){
var request;
$("#message").keypress(function (event) {
// First clear the interval if exists
if(request){
clearInterval(request);
}
// the set the interval in the same variable
request = setInterval(function(){
ws.send(JSON.stringify({"type": "typing", "from": {"id": client.self.id, "hash": client.self.hash}, "to": {"id": client.partner.id, "hash": client.partner.hash}}));
}, 3000);
});
});
祝你好运!!祝你快乐 好的,听起来很酷
那么这个,
- 你应该为你的时间间隔取一个变量
- 在按键时,检查该间隔是否存在,如果存在,则清除该间隔
- 然后在同一变量上设置间隔,以便在下一次按键时再次检查间隔是否存在,并可以重复这些步骤
$(function(){
var request;
$("#message").keypress(function (event) {
// First clear the interval if exists
if(request){
clearInterval(request);
}
// the set the interval in the same variable
request = setInterval(function(){
ws.send(JSON.stringify({"type": "typing", "from": {"id": client.self.id, "hash": client.self.hash}, "to": {"id": client.partner.id, "hash": client.partner.hash}}));
}, 3000);
});
});
祝你好运!!祝你快乐 好的,听起来很酷
那么这个,
- 你应该为你的时间间隔取一个变量
- 在按键时,检查该间隔是否存在,如果存在,则清除该间隔
- 然后在同一变量上设置间隔,以便在下一次按键时再次检查间隔是否存在,并可以重复这些步骤
$(function(){
var request;
$("#message").keypress(function (event) {
// First clear the interval if exists
if(request){
clearInterval(request);
}
// the set the interval in the same variable
request = setInterval(function(){
ws.send(JSON.stringify({"type": "typing", "from": {"id": client.self.id, "hash": client.self.hash}, "to": {"id": client.partner.id, "hash": client.partner.hash}}));
}, 3000);
});
});
祝你好运!!祝你快乐 这段代码有点冗长,可能写得更好,但它可以做到这一点。基本上,您希望在计时器运行时按一定间隔执行代码,并且需要在每次按键时清除/设置计时器。如果计时器熄灭,请停止所有操作:
这段代码有点冗长,可能写得更好,但它可以做到这一点。基本上,您希望在计时器运行时按一定间隔执行代码,并且需要在每次按键时清除/设置计时器。如果计时器熄灭,请停止所有操作:
这段代码有点冗长,可能写得更好,但它可以做到这一点。基本上,您希望在计时器运行时按一定间隔执行代码,并且需要在每次按键时清除/设置计时器。如果计时器熄灭,请停止所有操作:
这段代码有点冗长,可能写得更好,但它可以做到这一点。基本上,您希望在计时器运行时按一定间隔执行代码,并且需要在每次按键时清除/设置计时器。如果计时器熄灭,请停止所有操作:
当发生以下两种情况之一时,您将知道有人已停止键入:
blur
事件处理程序进行测试,或者setTimeout()
和clearTimeout()
进行测试var timeoutId,
intervalId;
function stopMyInterval() {
clearInterval(intervalId);
intervalId = null;
}
$("#message").keypress(function(event){
clearTimeout(timeoutId); // user is still typing, so cancel previous timeout
if (!intervalId) {
intervalId= setInterval(function() {
ws.send(JSON.stringify({"type": "typing", "from": {"id": client.self.id, "hash": client.self.hash}, "to": {"id": client.partner.id, "hash": client.partner.hash}}));
}, 3000);
}
timeoutId= setTimeout(stopMyInterval, 500);
// try some different values here -----^^^ until it feels natural.
}).blur(stopMyInterval);
请注意,在调用clearInterval()
/clearTimeout()
)之前,实际上不需要测试是否设置了间隔/超时(如果传递无效值,则两个函数都不会对象)
演示:当发生以下两种情况之一时,您将知道有人已停止键入:
blur
事件处理程序进行测试,或者setTimeout()
和clearTimeout()
进行测试var timeoutId,
intervalId;
function stopMyInterval() {
clearInterval(intervalId);
intervalId = null;
}
$("#message").keypress(function(event){
clearTimeout(timeoutId); // user is still typing, so cancel previous timeout
if (!intervalId) {
intervalId= setInterval(function() {
ws.send(JSON.stringify({"type": "typing", "from": {"id": client.self.id, "hash": client.self.hash}, "to": {"id": client.partner.id, "hash": client.partner.hash}}));
}, 3000);
}
timeoutId= setTimeout(stopMyInterval, 500);
// try some different values here -----^^^ until it feels natural.
}).blur(stopMyInterval);
请注意,在调用clearInterval()
/clearTimeout()
)之前,实际上不需要测试是否设置了间隔/超时(如果传递无效值,则两个函数都不会对象)
演示:当发生以下两种情况之一时,您将知道有人已停止键入:
blur
事件处理程序进行测试,或者setTimeout()
和clearTimeout()
进行测试var timeoutId,
intervalId;
function stopMyInterval() {
clearInterval(intervalId);
intervalId = null;
}
$("#message").keypress(function(event){
clearTimeout(timeoutId); // user is still typing, so cancel previous timeout
if (!intervalId) {
intervalId= setInterval(function() {
ws.send(JSON.stringify({"type": "typing", "from": {"id": client.self.id, "hash": client.self.hash}, "to": {"id": client.partner.id, "hash": client.partner.hash}}));
}, 3000);
}
timeoutId= setTimeout(stopMyInterval, 500);
// try some different values here -----^^^ until it feels natural.
}).blur(stopMyInterval);
请注意,在调用clearInterval()
/clearTimeout()
)之前,实际上不需要测试是否设置了间隔/超时(如果传递无效值,则两个函数都不会对象)
演示:当发生以下两种情况之一时,您将知道有人已停止键入:
blur
事件处理程序进行测试,或者