How to detect idle time in JavaScript elegantly?

前端 未结 30 2417
别跟我提以往
别跟我提以往 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:36

    I have tested this code working file:

    var timeout = null;
        var timee = '4000'; // default time for session time out.
        $(document).bind('click keyup mousemove', function(event) {
    
        if (timeout !== null) {
                clearTimeout(timeout);
            }
            timeout = setTimeout(function() {
                  timeout = null;
                console.log('Document Idle since '+timee+' ms');
                alert("idle window");
            }, timee);
        });
    

提交回复
热议问题