Create a gist now

Instantly share code, notes, and snippets.

@KrofDrakula /raf.js
Last active Dec 15, 2015

What would you like to do?
Due to a bug in the native `requestAnimationFrame` function running inside `<iframe>` elements on iOS 6, I've decided to write up a custom function that implements a blacklist. Use this snippet and use `nextFrame` and `cancelFrame` on the `window` object to use this workaround with the same method signature as the spec. I've avoided overriding t…
// requestAnimationFrame implementation as a custom function to allow blacklisting
// devices with broken implementation. Currently needs timer-based fallbacks for iOS 6.x for
// code running inside <iframe> elements.
// Uses polyfills based on
(function() {
var blacklisted = /iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent),
lastTime = 0,
nativeRequest = window.requestAnimationFrame || null,
nativeCancel = window.cancelAnimationFrame || null;
['webkit', 'moz'].forEach(function(prefix) {
nativeRequest = nativeRequest || window[prefix+'RequestAnimationFrame'] || null;
nativeCancel = nativeCancel || window[prefix+'CancelAnimationFrame'] || window[prefix+'CancelRequestAnimationFrame'] || null;
function polyfillRequest(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall);
lastTime = currTime + timeToCall;
return id;
function polyfillCancel(id) {
this.nextFrame = (!blacklisted && nativeRequest != null) ? nativeRequest : polyfillRequest;
this.cancelFrame = (!blacklisted && nativeCancel != null) ? nativeCancel : polyfillCancel;

I can confirm this fixed a bug on ios6 where raf would not run on subsequent pages after being invoked on one page. (no iframe, just simple page to page navigation) Same symptoms with the address bar as you have described (click on url bar then back to page and raf runs ok). I was surprised how little info came up on this. Thanks for the code.

ynk commented Aug 6, 2013

Thank you so much.
It's fixed a bug i was trying to find for 48h straight.

kof commented Sep 19, 2013

Can't you feature test it instead of version sniff? f.e. request once, if called back - then it works?

kof commented Sep 21, 2013

I have found a workaround: using top.requestAnimationFrame. Does it works for you guys? I mean within an iframe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment