-3

我正在使用 programO(我是 php 新手)制作一个采访聊天机器人,如果很长时间没有聊天窗口上的活动,我想在其中显示一条消息。我已经搜索了 jquery 插件,但它们用于检测鼠标或键盘敲击,但我只是关心检测聊天窗口是否更新了用户输入。我还搜索了 javascript settimeout 和 settimeinterval 函数来显示消息,但我无法理解如何将它们与聊天窗口表单一起使用。

4

1 回答 1

0

setTimeout 需要与 clearTimeout 结合使用。每当发生时间重置事件时使用 clearTimeout。

例子:

<script>
    var interval = -1;
    var TIMEOUT = 5; // seconds
    function doTimeout(){
        alert('show timeout message');
        startClock(); // if you want to restart the timer
    }
    function startClock(event){
        console.log("foo",interval, event);
        if(interval !== -1){
            console.log("clearing timeout");
            clearTimeout(interval);
        }
        interval = setTimeout(doTimeout,TIMEOUT*1000);
    }
    startClock(); // if you want to start the timer on arrival
</script>

<!-- add remove events as you see fit -->
<textarea id="chatinput" onblur="startClock(event)" onfocus="startClock(event)" onchange="startClock(event)" onkeyup="startClock(event)"></textarea>

有关超时的更多信息:
标签:
问题:javascript setTimeout clearTimeout

于 2012-05-31T15:18:52.407 回答