How to detect idle time in JavaScript elegantly?

前端 未结 30 2373
别跟我提以往
别跟我提以往 2020-11-21 11:51

Is it possible to detect \"idle\" time in JavaScript?
My primary use case probably would be to pre-fetch or preload content.

Idle time:

30条回答
  •  遇见更好的自我
    2020-11-21 12:56

    You can use the below mentioned solution

    var idleTime;
    $(document).ready(function () {
             reloadPage();
            $('html').bind('mousemove click mouseup mousedown keydown keypress keyup submit change mouseenter scroll resize dblclick', function () {
                clearTimeout(idleTime);
                reloadPage();
            });
    });
    function reloadPage() {
        clearTimeout(idleTime);
        idleTime = setTimeout(function () {
            location.reload();
        }, 3000);
    }
    

提交回复
热议问题