Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestAnimationFrame instead of setTimeOut #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 44 additions & 14 deletions jquery.ba-resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@
$.data( this, str_data, { w: elem.width(), h: elem.height() } );

// If this is the first element added, start the polling loop.
if ( elems.length === 1 ) {
if ( elems.length === 1 ) {
//set the timeout_id to undefined.
timeout_id = undefined;

//start the loop
loopy();
}
},
Expand All @@ -168,7 +172,10 @@

// If this is the last element removed, stop the polling loop.
if ( !elems.length ) {
clearTimeout( timeout_id );
cancelAnimationFrame( timeout_id );

//set the timeout_id to null, to make sure the loop is stopped
timeout_id = null;
}
},

Expand Down Expand Up @@ -216,11 +223,7 @@

};

function loopy() {

// Start the polling loop, asynchronously.
timeout_id = window[ str_setTimeout ](function(){

function loopy() {
// Iterate over all elements to which the 'resize' event is bound.
elems.each(function(){
var elem = $(this),
Expand All @@ -235,12 +238,39 @@
}

});

// Loop.
loopy();

}, jq_resize[ str_delay ] );


//request another animationFrame to poll the elements
if(timeout_id !== null)
timeout_id = window.requestAnimationFrame(loopy);
};

})(jQuery,this);
/**
* Provides requestAnimationFrame in a cross browser way.
* http://paulirish.com/2011/requestanimationframe-for-smart-animating/
*/
if ( !window.requestAnimationFrame ) {
window.requestAnimationFrame = (function(){
return window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
return window.setTimeout( callback, jq_resize[ str_delay ] );
};
})();
}
/**
* Provides cancelAnimationFrame in a cross browser way.
*/
if( !window.cancelAnimationFrame ){
window.cancelAnimationFrame = ( function() {
return window.webkitCancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame ||
window.oCancelRequestAnimationFrame ||
window.msCancelRequestAnimationFrame ||
clearTimeout
})();
}


})(jQuery,this);
4 changes: 2 additions & 2 deletions jquery.ba-resize.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.