How to detect idle time in JavaScript elegantly?

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

    I wrote a simple jQuery plugin that will do what you are looking for.

    https://github.com/afklondon/jquery.inactivity

    $(document).inactivity( {
        interval: 1000, // the timeout until the inactivity event fire [default: 3000]
        mouse: true, // listen for mouse inactivity [default: true]
        keyboard: false, // listen for keyboard inactivity [default: true]
        touch: false, // listen for touch inactivity [default: true]
        customEvents: "customEventName", // listen for custom events [default: ""]
        triggerAll: true, // if set to false only the first "activity" event will be fired [default: false]
    });
    

    The script will listen for mouse, keyboard, touch and other custom events inactivity (idle) and fire global "activity" and "inactivity" events.

    Hope this helps :)

提交回复
热议问题