How can I detect javascript idleness?

Does anyone know how I can write a Javacript function that returns true if the browser Javascript engine has been idle for some time?

I am happy to use mootools / jQuery etc. if it is easier, with a slight advantage for mootools.

EDIT:

My actual problem: I am calling a third-party API. The call returns immediately, but the actual work done with the DOM continues for some time (> 10 seconds in many cases). I want some of my code to run as soon as the DOM is finished. I don’t know anything about the internal functions of the API function that I am calling, so I was looking for a general way to determine the completion of this function.

+4
source share
5 answers

If you call such a function, will not any idle time counter reset if one exists?

If you can outline the problem you are trying to solve, perhaps we can give you a hand. I can think of how I approach the solution of the “how much time has passed since the moment of this function” or “how much time has passed since the last event on the page” or “do something if the user has been on this page for X seconds" but I don’t have a good way to do this if nothing happened in X seconds. "

Update

Based on your update, I would look for a callback mechanism in the API (could you share what it is?) That will allow you to schedule some work after the API finishes. If this does not work, you can try adding something like:

setTimeout( function() { myCallbackMethod(); }, 0 ); 

This will cause your callback to begin after the current execution completes. It depends on the fact that the javascript mechanism is single-threaded, but I saw that it is used for a good effect.

+3
source

There are two facts that you can use to measure the inactivity of a javascript / flash stream.

  • Javascript has synchronization sources ( setTimeout, setInterval ), which are marked only in the absence of javascript. Thus, the less the flow is idle, the slower the timer.

    “JavaScript can only execute one piece of code at a time (due to its single-threaded nature), each of which blocks of code“ blocks ”the progress of other asynchronous events. This means that when an asynchronous event occurs (for example, a mouse click, timer, or XMLHttpRequest termination ), it is queued to be executed later. " How JavaScript Timers Work

  • Javascript has an external sync source that ticks regardless of idleness. An instance of the Date () class is based on an external hour, you can get a millisecond time selection using getTime () :

    var timeSinceDisco = (new date) .getTime ();

eJohn has some interesting tests using getTime () for benchmarking .

We use the difference between the external timer Date.getTime () and the internal time setTimeout to calculate the degree of inactivity of the javascript stream. Of course, this will not work on sexy new javascript engines like V8 or tracemonkey , as they use more than one thread. It should work for most browsers that currently exist.

Gmail Tutorials: Using Timers Effectively

0
source

I am 99.4% sure that there is no such function, however, I would like it because I really need it.

Of course, you can create your own solution here (the function that you call at the beginning of each function of the entry point), depending on the details, there may be low overhead.

 var Tracker={ last: new Date().geTime(), activity:function() { this.last=new Date().geTime(); }, idle:function() { return new Date().geTime()-this.last; } } ... var clickedSomething=function() { Tracker.activity(); ...real code.. } 
0
source

You can call the function to check the X and Y positions of the mouse for each timer interval and compare them with the last saved X and Y positions, if they are the same, therefore it is idle.

MooTools example

0
source
0
source

All Articles