Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / resources / src / mediawiki / mediawiki.requestIdleCallback.js
blob796639f9bf530e5b47a2a66d4ae7e531043054ba
1 /*!
2  * An interface for scheduling background tasks.
3  *
4  * Loosely based on https://w3c.github.io/requestidlecallback/
5  */
6 ( function ( mw, $ ) {
7         var tasks = [],
8                 maxIdleDuration = 50,
9                 timeout = null;
11         function schedule( trigger ) {
12                 clearTimeout( timeout );
13                 timeout = setTimeout( trigger, 700 );
14         }
16         function triggerIdle() {
17                 var elapsed,
18                         start = mw.now();
20                 while ( tasks.length ) {
21                         elapsed = mw.now() - start;
22                         if ( elapsed < maxIdleDuration ) {
23                                 tasks.shift().callback();
24                         } else {
25                                 // Idle moment expired, try again later
26                                 schedule( triggerIdle );
27                                 break;
28                         }
29                 }
30         }
32         mw.requestIdleCallbackInternal = function ( callback ) {
33                 var task = { callback: callback };
34                 tasks.push( task );
36                 $( function () { schedule( triggerIdle ); } );
37         };
39         /**
40          * Schedule a deferred task to run in the background.
41          *
42          * @member mw
43          * @param {Function} callback
44          */
45         mw.requestIdleCallback = window.requestIdleCallback
46                 ? function ( callback ) {
47                         window.requestIdleCallback( callback );
48                 }
49                 : mw.requestIdleCallbackInternal;
50 }( mediaWiki, jQuery ) );