Skip to content

Commit 9cb81ed

Browse files
authored
Merge pull request #1039 from web-bert/stop-requestAnim-global
Fix leak of global vars in polyfill
2 parents 7db1b00 + 38bc155 commit 9cb81ed

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

dist/extras/request_animation_frame.js

+36-23
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,41 @@
1515

1616
// MIT license
1717

18+
(function(){
19+
20+
'use strict';
1821

19-
'use strict';
20-
21-
if(!Date.now) {
22-
Date.now = function() { return new Date().getTime(); };
23-
}
24-
25-
var vendors = ['webkit', 'moz'];
26-
for(var i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
27-
var vp = vendors[i];
28-
window.requestAnimationFrame = window[vp + 'RequestAnimationFrame'];
29-
window.cancelAnimationFrame = (window[vp + 'CancelAnimationFrame'] ||
30-
window[vp + 'CancelRequestAnimationFrame']);
31-
}
32-
if(/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) || // iOS6 is buggy
33-
!window.requestAnimationFrame || !window.cancelAnimationFrame) {
22+
if(!Date.now) {
23+
Date.now = function() { return (new Date()).getTime(); };
24+
}
25+
26+
var vendors = ['webkit', 'moz'];
27+
var i = 0;
28+
var l = vendors.length;
29+
var vp;
3430
var lastTime = 0;
35-
window.requestAnimationFrame = function(callback) {
36-
var now = Date.now();
37-
var nextTime = Math.max(lastTime + 16, now);
38-
return setTimeout(function() { callback(lastTime = nextTime); },
39-
nextTime - now);
40-
};
41-
window.cancelAnimationFrame = clearTimeout;
42-
}
31+
32+
for(; i < l && !window.requestAnimationFrame; ++i) {
33+
vp = vendors[i];
34+
window.requestAnimationFrame = window[vp + 'RequestAnimationFrame'];
35+
window.cancelAnimationFrame = (window[vp + 'CancelAnimationFrame'] ||
36+
window[vp + 'CancelRequestAnimationFrame']);
37+
}
38+
39+
if(/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) || // iOS6 is buggy
40+
!window.requestAnimationFrame || !window.cancelAnimationFrame) {
41+
window.requestAnimationFrame = function(callback) {
42+
var now = Date.now();
43+
var nextTime = Math.max(lastTime + 16, now);
44+
return setTimeout(function() { callback(lastTime = nextTime); },
45+
nextTime - now);
46+
};
47+
window.cancelAnimationFrame = clearTimeout;
48+
}
49+
50+
vendors = null;
51+
i = null;
52+
l = null;
53+
vp = null;
54+
55+
}());

0 commit comments

Comments
 (0)