Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在调用事件时每隔几秒钟运行一次代码_Javascript_Jquery - Fatal编程技术网

Javascript 在调用事件时每隔几秒钟运行一次代码

Javascript 在调用事件时每隔几秒钟运行一次代码,javascript,jquery,Javascript,Jquery,我正在创建一个聊天脚本,在有人打字时,我需要每三秒运行一次代码。我可以很容易地使用setInterval每三秒钟运行一次代码,但是将setInterval放在我的按键事件中会导致它在每次按键时都从一开始就开始运行,所以它永远不会运行,直到他们停止键入为止。另一个问题是当他们停止打字时停止它。有什么想法吗 $("#message").keypress(function(event){ // Do stuff here every keypress // Every three s

我正在创建一个聊天脚本,在有人打字时,我需要每三秒运行一次代码。我可以很容易地使用setInterval每三秒钟运行一次代码,但是将setInterval放在我的按键事件中会导致它在每次按键时都从一开始就开始运行,所以它永远不会运行,直到他们停止键入为止。另一个问题是当他们停止打字时停止它。有什么想法吗

$("#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}}));
});
好的,听起来很酷

那么这个,

  • 你应该为你的时间间隔取一个变量
  • 在按键时,检查该间隔是否存在,如果存在,则清除该间隔
  • 然后在同一变量上设置间隔,以便在下一次按键时再次检查间隔是否存在,并可以重复这些步骤
这是POC:

$(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);
    });
});
祝你好运!!祝你快乐

好的,听起来很酷

那么这个,

  • 你应该为你的时间间隔取一个变量
  • 在按键时,检查该间隔是否存在,如果存在,则清除该间隔
  • 然后在同一变量上设置间隔,以便在下一次按键时再次检查间隔是否存在,并可以重复这些步骤
这是POC:

$(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);
    });
});
祝你好运!!祝你快乐

好的,听起来很酷

那么这个,

  • 你应该为你的时间间隔取一个变量
  • 在按键时,检查该间隔是否存在,如果存在,则清除该间隔
  • 然后在同一变量上设置间隔,以便在下一次按键时再次检查间隔是否存在,并可以重复这些步骤
这是POC:

$(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);
    });
});
祝你好运!!祝你快乐

好的,听起来很酷

那么这个,

  • 你应该为你的时间间隔取一个变量
  • 在按键时,检查该间隔是否存在,如果存在,则清除该间隔
  • 然后在同一变量上设置间隔,以便在下一次按键时再次检查间隔是否存在,并可以重复这些步骤
这是POC:

$(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
    事件处理程序进行测试,或者
  • 在x毫秒内没有发生关键事件,x是相当主观的,输入速度不同,但可能是500毫秒左右。您可以在密钥处理程序中使用
    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
    事件处理程序进行测试,或者
  • 在x毫秒内没有发生关键事件,x是相当主观的,输入速度不同,但可能是500毫秒左右。您可以在密钥处理程序中使用
    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
    事件处理程序进行测试,或者
  • 在x毫秒内没有发生关键事件,x是相当主观的,输入速度不同,但可能是500毫秒左右。您可以在密钥处理程序中使用
    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
    事件处理程序进行测试,或者
  • x毫秒内未发生任何关键事件,w