Javascript timeout when no actions from user for specified time

后端 未结 7 967
广开言路
广开言路 2020-12-28 19:32

I want to call a js function when there is no activity from user on the web page for specified amount of time. If there is activity from user then reset timeout. I tried to

7条回答
  •  有刺的猬
    2020-12-28 20:17

    You want to monitor events like mousemove, keypress, keydown, and/or click at the document level.

    Edit: This being a smartphone app changes what events you want to listen for. Given your textbox and button requirements, I'd listen to oninput and then add the resetTimeout() call to the click handler for your button.

    var inactivityTimeout = 0;
    function resetTimeout() {
        clearTimeout(inactivityTimeout);
        inactivityTimeout = setTimeout(inactive, 300000);
    }
    function inactive() {
       ...
    }
    document.getElementById("chatInput").oninput = resetTimeout;
    

提交回复
热议问题