How to detect idle time in JavaScript elegantly?

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

    I know it a relatively old question, but I had the same issue and I found a quite good solution.

    I used: jquery.idle and I only needed to do:

    $(document).idle({
      onIdle: function(){
        alert('You did nothing for 5 seconds');
      },
      idle: 5000
    })
    

    See JsFiddle demo.

    (Just for Info: see this for back-end event tracking Leads browserload)

提交回复
热议问题