Skip to content

Commit a7c3c3a

Browse files
author
aFarkas
committed
fix old chrome implementation
1 parent 844b9b8 commit a7c3c3a

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

index.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
var tasks = [];
2020
var runAttempts = 0;
2121
var isRunning = false;
22-
var remainingTime = 25;
23-
var minThrottle = 0;
24-
var throttle = 0;
22+
var remainingTime = 7;
23+
var minThrottle = 35;
24+
var throttle = 125;
2525
var index = 0;
2626
var taskStart = 0;
2727
var tasklength = 0;
@@ -36,7 +36,7 @@
3636
};
3737
var setInacitve = debounce(function(){
3838
remainingTime = 25;
39-
throttle = 0;
39+
throttle = 33;
4040
minThrottle = 0;
4141
});
4242

@@ -110,7 +110,7 @@
110110
throttleDelay = minThrottle;
111111
}
112112

113-
if(throttleDelay > 0){
113+
if(throttleDelay > 9){
114114
lazytimer = setTimeout(scheduleRaf, throttleDelay);
115115
} else {
116116
throttleDelay = 0;
@@ -142,23 +142,23 @@
142142
}
143143
}
144144

145-
function requestIdleCallback(task){
145+
function requestIdleCallbackShim(task){
146146
index++;
147147
tasks.push(task);
148148
scheduleLazy();
149149
return index;
150150
}
151151

152-
function cancelIdleCallback(id){
152+
function cancelIdleCallbackShim(id){
153153
var index = id - 1 - tasklength;
154154
if(tasks[index]){
155155
tasks[index] = null;
156156
}
157157
}
158158

159159
if(!root.requestIdleCallback || !root.cancelIdleCallback){
160-
root.requestIdleCallback = requestIdleCallback;
161-
root.cancelIdleCallback = cancelIdleCallback;
160+
root.requestIdleCallback = requestIdleCallbackShim;
161+
root.cancelIdleCallback = cancelIdleCallbackShim;
162162

163163
window.addEventListener('scroll', onInputorMutation, true);
164164
window.addEventListener('resize', onInputorMutation);
@@ -170,10 +170,35 @@
170170
if(window.MutationObserver){
171171
new MutationObserver( onInputorMutation ).observe( document.documentElement, {childList: true, subtree: true, attributes: true} );
172172
}
173+
} else {
174+
try{
175+
root.requestIdleCallback(function(){}, {timeout: 0});
176+
} catch(e){
177+
(function(rIC){
178+
var timeRemainingProto, timeRemaining;
179+
root.requestIdleCallback = function(fn, timeout){
180+
if(timeout && typeof timeout.timeout == 'number'){
181+
return rIC(fn, timeout.timeout);
182+
}
183+
return rIC(fn);
184+
};
185+
if(root.IdleCallbackDeadline && (timeRemainingProto = IdleCallbackDeadline.prototype)){
186+
timeRemaining = Object.getOwnPropertyDescriptor(timeRemainingProto, 'timeRemaining');
187+
if(!timeRemaining.configurable || !timeRemaining.get){return;}
188+
Object.defineProperty(timeRemainingProto, 'timeRemaining', {
189+
value: function(){
190+
return timeRemaining.get.call(this);
191+
},
192+
enumerable: true,
193+
configurable: true,
194+
});
195+
}
196+
})(root.requestIdleCallback)
197+
}
173198
}
174199

175200
return {
176-
request: requestIdleCallback,
177-
cancel: cancelIdleCallback,
201+
request: requestIdleCallbackShim,
202+
cancel: cancelIdleCallbackShim,
178203
};
179204
}));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "requestidlecallback",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "A polyfill for the requestIdleCallback.",
55
"main": "index.js",
66
"filename": "index.js",

0 commit comments

Comments
 (0)