2 * An interface for scheduling background tasks.
4 * Loosely based on https://w3c.github.io/requestidlecallback/
11 function schedule( trigger ) {
12 clearTimeout( timeout );
13 timeout = setTimeout( trigger, 700 );
16 function triggerIdle() {
20 while ( tasks.length ) {
21 elapsed = mw.now() - start;
22 if ( elapsed < maxIdleDuration ) {
23 tasks.shift().callback();
25 // Idle moment expired, try again later
26 schedule( triggerIdle );
32 mw.requestIdleCallbackInternal = function ( callback ) {
33 var task = { callback: callback };
36 $( function () { schedule( triggerIdle ); } );
40 * Schedule a deferred task to run in the background.
43 * @param {Function} callback
45 mw.requestIdleCallback = window.requestIdleCallback
46 ? function ( callback ) {
47 window.requestIdleCallback( callback );
49 : mw.requestIdleCallbackInternal;
50 }( mediaWiki, jQuery ) );