Using requestAnimationFrame and a target FPS

By on

Combining setInterval and requestAnimationFrame to get a target framerate

In the beginning...

People who've worked with Javascript animations before will probably know the next piece of code:
var targetFps = 25;
var loopInterval = window.setInterval(renderFrame, 1000 / targetFps);

What this does is create a loop that will run 25 times per second, which will hopefully give you your target framerate of 25 frames per second.

And They said: Do not use setInterval!

But then the powers that be thought of a new api: requestAnimatonFrame. It's basically a function that tells the browser it needs to execute something which will result in a redraw of a certain element.

We were told to set it up in a function like this:

function renderLoop(){
    // Put another iteration of renderLoop in the animation queue
    window.requestAnimationFrame(renderLoop);
    
    // Actually calculate and draw a frame
    renderFrame();
}

Yes, you're reading it correctly: it's basically a function calling itself. This will give you the best possible framerate. But what if we could combine those 2: setInterval and requestAnimationFrame?

The best of both worlds

function renderLoop(){

    var targetFps = 25;	
    
    // Combine setInterval and requestAnimationFrame in order to get a desired fps
    setInterval(function() {
        window.requestAnimationFrame(renderFrame);
    }, 1000 / targetFps);
}

And now you have a working requestAnimationFrame setup with a target fps!

So, what do you think?

A requestAnimationFrame polyfill

Warning: you will probably want to use a fallback function. Current fallback functions default to setInterval itself, which is not what we want.

Here's a very basic one you can use:

window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       || 
		  window.webkitRequestAnimationFrame ||
		  window.mozRequestAnimationFrame    || 
		  window.oRequestAnimationFrame      || 
		  window.msRequestAnimationFrame     || 
		  function(/* function */ callback, /* DOMElement */ element){

			// Since our requestAnimFrame is already in a loop in order to
			// control the preferred FPS, just call callback, not an interval
			callback();
		  };
})();

Comments

juliadiets.com

This is also a well known problem in the professional video industry when you want to playback a video at a different FPS then the device showing it refresh at. Many techniques has been used such as frame blending and complex re-timing re-building intermediate frames based on motion vectors, but with canvas these techniques are not available and the result will always be jerky video.

hlfrmxsx

01/01/1967

please visit giochi

Some really excellent information, Gladiolus I discovered this.

fashion victim

Excellent web site you have here.. It's difficult too find quality writimg like yours nowadays. I truly appreciate individuals like you! Take care!!

MuillNot

Why on earth should I do it? Leave him alone, he’s out of sorts today

MuillNot

Whatever the case I’ll always be subscribing to your entire supply and I hope you jot down ever again soon! I went to the show to kill time.

imitation youth jersey

Buut wanna remark that you have a very decent internet site, I like the layout it actually stands out.

check cheap

Thanks for sharing your thoughtts about crate training cheap. Regards

Johny Jackson

great points altogether, you simply won a new reader. What might you suggest in regards to your publish that you simply made a few days ago? Any positive?

ProFunding247

Quicker and Simpler than the SBA, http://ProFunding247.com can get your business a loan for $2K-350,000 With low-credit and no collateral.

Use our 1 minute form to See exactly how much you can get, No-Cost:

http://ProFunding247.com

If you've been established for at least 12 months you are already pre-qualified. Our Fast service means funding can be finished within 48 hours. Terms are specific for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds have no Restrictions, allowing you to use the whole amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://ProFunding247.com

Have a great day, The Pro Funding 24-7 Team

unsubscribe here - http://profunding247.com/i.php?url/r.php?url=skerit.com&id=e98 10685-B Hazelhurst Dr. # 21465 Houston, TX 77043

FastFunding365

Hi, letting you know that http://FastFunding365.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://FastFunding365.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://FastFunding365.com

Have a great day, The Fast Funding 365 Team

unsubscribe/remove - http://FastFunding365.com/r.php?url=skerit.com&id=e102

FastFundingAdvisors

Hi, letting you know that http://FastFundingAdvisors.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://FastFundingAdvisors.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://FastFundingAdvisors.com

Have a great day, The Fast Funding Advisors Team

unsubscribe/remove - http://FastFundingAdvisors.com/r.php?url=skerit.com&id=e102

GetaBusinessLoanNow

Faster and Easier than the SBA, http://GetaBusinessLoanNow.com can get your business a loan for $2K-350,000 With low-credit and no collateral.

Use our quick form to See exactly how much you can get, No-Cost:

http://GetaBusinessLoanNow.com

If you've been established for at least 1 year you are already pre-qualified. Our Fast service means funding can be completed within 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds have no Restrictions, allowing you to use the whole amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://GetaBusinessLoanNow.com

Have a great day, The Get a Business Loan Now Team

http://GetaBusinessLoanNow.com/r.php?url=skerit.com&id=e105

GetaBusinessFunded247

Hi, letting you know that http://GetaBusinessFunded-24-7.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://GetaBusinessFunded-24-7.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://GetaBusinessFunded-24-7.com

Have a great day, The Get a Business Funded 247 Team

unsubscribe/remove - http://GetaBusinessFunded-24-7.com/r.php?url=skerit.com&id=e104

BusinessCapitalAdvisor365

Faster and Easier than the SBA, http://BusinessCapitalAdvisor365.com can get your business a loan for $2K-350,000 With low-credit and no collateral.

Use our fast form to See exactly how much you can get, No-Cost:

http://BusinessCapitalAdvisor365.com

If you've been in business for at least 12 months you are already pre-qualified. Our Fast service means funding can be completed within 48hrs. Terms are specific for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds have no Restrictions, allowing you to use the whole amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://BusinessCapitalAdvisor365.com

Have a great day, The Business Capital Advisor 365 Team

http://BusinessCapitalAdvisor365.com/r.php?url=skerit.com&id=e105

ProFunder247

Faster and Simpler than the SBA, http://ProFunder-247.com can get your business a loan for $2K-350,000 With low-credit and without collateral.

Use our quick form to Find Out exactly how much you can get, No-Cost:

http://ProFunder-247.com

If you've been in business for at least 12 months you are already pre-qualified. Our Fast service means funding can be finished within 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds are also Non-Restrictive, allowing you to use the whole amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://ProFunder-247.com

Have a great day, The Pro Funder 247 Team

http://ProFunder-247.com/r.php?url=skerit.com&id=e107

BusinessFunding365

Hi, letting you know that http://Business-Funding-365.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://Business-Funding-365.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://Business-Funding-365.com

Have a great day, The Business Funding 365 Team

unsubscribe/remove - http://Business-Funding-365.com/r.php?url=skerit.com&id=e108

FastFundingAdvisor

Faster and Simpler than the SBA, http://Fast-Funding-Advisor.com can get your business a loan for $2K-350,000 With low-credit and without collateral.

Use our quick form to Find Out exactly how much you can get, No-Cost:

http://Fast-Funding-Advisor.com

If you've been in business for at least 1 year you are already pre-qualified. Our Quick service means funding can be completed within 48 hours. Terms are specific for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds are also Non-Restrictive, allowing you to use the full amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://Fast-Funding-Advisor.com

Have a great day, The Fast Funding Advisor Team

remove here - http://Fast-Funding-Advisor.com/r.php?url=skerit.com&id=e109

FastFundingAdvisors

Hi, letting you know that http://Fast-Funding-Advisors.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://Fast-Funding-Advisors.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://Fast-Funding-Advisors.com

Have a great day, The Fast Funding Advisors Team

unsubscribe/remove - http://Fast-Funding-Advisors.com/r.php?url=skerit.com&id=e110

ProFunding247

Quicker and Simpler than the SBA, http://ProFunding247.biz can get your business a loan for $2K-350,000 With low-credit and no collateral.

Use our 1 minute form to See exactly how much you can get, No-Cost:

http://ProFunding247.biz

If you've been in business for at least 1 year you are already pre-qualified. Our Fast service means funding can be finished within 48 hours. Terms are specific for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds are also Non-Restrictive, allowing you to use the whole amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://ProFunding247.biz

Have a great day, The Pro Funding 247 Team

remove here - http://ProFunding247.biz/r.php?url=skerit.com&id=e111

minecraft

Right here is the right site for everyone who hopes to understand this topic. You realize a whole lot its almost hard to argue with you (not that I actually would want to…HaHa). You definitely put a new spin on a topic that has been written about for ages. Excellent stuff, just wonderful!

minecraft

always i used to read smaller posts that as well clear their motive, and that is also happening with this paragraph which I am reading here.

minecraft

I don't know if it's just me or if perhaps everyone else experiencing issues with your website. It appears as if some of the written text on your content are running off the screen. Can somebody else please provide feedback and let me know if this is happening to them too? This could be a issue with my internet browser because I've had this happen previously. Many thanks

GetaBusinessFunded247

Hi, letting you know that http://GetaBusinessFunded-24-7.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://GetaBusinessFunded-24-7.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://GetaBusinessFunded-24-7.com

Have a great day, The Get a Business Funded 247 Team

unsubscribe/remove - http://GetaBusinessFunded-24-7.com/r.php?url=skerit.com&id=e114

minecraft

Nice respond in return of this question with firm arguments and telling the whole thing on the topic of that.

minecraft

Hi there to every body, it's my first visit of this blog; this web site includes remarkable and actually excellent stuff designed for readers.

minecraft

Hello, this weekend is fastidious for me, since this moment i am reading this enormous informative article here at my home.

FastFunding365

Quicker and Easier than the SBA, http://FastFunding365.com can get your business a loan for $2K-350,000 With low-credit and without collateral.

Use our fast form to Find Out exactly how much you can get, No-Cost:

http://FastFunding365.com

If you've been in business for at least 12 months you are already pre-qualified. Our Quick service means funding can be completed within 48 hours. Terms are personalized for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds have no Restrictions, allowing you to use the full amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://FastFunding365.com

Have a great day, The Fast Funding 365 Team

remove here - http://FastFunding365.com/r.php?url=skerit.com&id=e115

minecraft

Thanks , I've recently been looking for information about this subject for ages and yours is the greatest I've found out till now. However, what about the conclusion? Are you positive concerning the source?

minecraft

Excellent post. I was checking continuously this blog and I'm impressed! Extremely useful information specifically the final part :) I care for such information much. I used to be seeking this certain information for a long time. Thank you and best of luck.

minecraft

Excellent article. Keep writing such kind of info on your site. Im really impressed by it. Hi there, You've done a fantastic job. I will definitely digg it and in my view recommend to my friends. I am confident they'll be benefited from this site.

minecraft

What's up to every single one, it's actually a nice for me to pay a quick visit this web page, it consists of precious Information.

minecraft

Appreciating the time and energy you put into your site and in depth information you present. It's nice to come across a blog every once in a while that isn't the same outdated rehashed information. Wonderful read! I've bookmarked your site and I'm including your RSS feeds to my Google account.

minecraft

I'm amazed, I have to admit. Seldom do I encounter a blog that's both educative and entertaining, and let me tell you, you've hit the nail on the head. The problem is something which not enough men and women are speaking intelligently about. I'm very happy that I found this during my hunt for something regarding this.

minecraft

What's up everybody, here every one is sharing such know-how, thus it's pleasant to read this website, and I used to go to see this blog everyday.

minecraft

Great blog you have here but I was curious about if you knew of any community forums that cover the same topics discussed here? I'd really like to be a part of online community where I can get suggestions from other knowledgeable people that share the same interest. If you have any suggestions, please let me know. Cheers!

minecraft

What's up to all, it's really a nice for me to pay a visit this web site, it contains priceless Information.

minecraft

Remarkable things here. I'm very satisfied to peer your post. Thank you so much and I'm taking a look ahead to touch you. Will you please drop me a mail?

minecraft

What's up it's me, I am also visiting this site daily, this web page is in fact fastidious and the users are actually sharing nice thoughts.

gamefly free trial

I was curious if you ever thought of changing the structure of your blog? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or two pictures. Maybe you could space it out better?

how to get help in windows 10

What's up, the whole thing is going sound here and ofcourse every one is sharing facts, that's in fact good, keep up writing.

ETIAS

If some one wishes expert view about blogging after that i advise him/her to pay a quick visit this web site, Keep up the fastidious job.

ETIAS Visa

Excellent blog here! Also your website a lot up fast! What host are you the usage of? Can I get your associate link on your host? I desire my website loaded up as quickly as yours lol

sms marketing dubai

I do consider all the ideas you've offered for your post. They're really convincing and can definitely work. Nonetheless, the posts are too brief for starters. May you please prolong them a bit from next time? Thanks for the post.

ESTA Visa

If you would like to get much from this piece of writing then you have to apply such techniques to your won webpage.

ESTA Visa

What's up all, here every one is sharing these kinds of experience, therefore it's pleasant to read this weblog, and I used to go to see this webpage everyday.

Dorothyslumn

We would like to inform that you liked a comment ID:35915743 in a social network , January 9, 2019 at 19:48 This like has been randomly selected to win the seasonal «Like Of The Year» 2019 award! http://facebook.com+prize+@1310252231/P7mvr

viagra

I'm amazed, I have to admit. Seldom do I come across a blog that's both equally educative and amusing, and let me tell you, you've hit the nail on the head. The issue is an issue that too few folks are speaking intelligently about. I am very happy that I came across this during my hunt for something concerning this.

Bail Bonds in Tampa Florida

Quality content is the main to be a focus for the users to pay a visit the web page, that's what this web site is providing.

Felony Bonds Tampa

If some one desires to be updated with latest technologies after that he must be pay a visit this web page and be up to date everyday.

pest control for ants

It's in fact very difficult in this full of activity life to listen news on TV, therefore I only use web for that reason, and get the newest information.

pest control for rats

I pay a quick visit each day some web pages and sites to read articles, except this blog gives quality based posts.

esta

I have fun with, result in I found just what I was taking a look for. You've ended my 4 day lengthy hunt!

God Bless you man. Have a great day. Bye

togel yang sudah keluar

If some one wants to be updated with most recent technologies afterward he must be visit this website and be up to date all the time.

bulk sms uae

Hi there mates, how is the whole thing, and what you wish for to say concerning this piece of writing, in my view its in fact remarkable for me.

DorothyQueli

We would like to inform that you liked a comment ID:35915743 in a social network , January 9, 2019 at 19:48 This like has been randomly selected to win the seasonal «Like Of The Year» 2019 award! http://facebook.com+prize+@1310252231/Xe9Bo9

here

WOW just what I was searching for. Came here by searching for private investigator

cbd soft gels

Yes! Finally something about private investigator.

plenty of fish dating site

Excellent weblog right here! Additionally your site lots up fast! What web host are you the usage of? Can I am getting your affiliate hyperlink to your host? I want my site loaded up as fast as yours lol

racism

You could definitely see your skills in the article you write. The world hopes for more passionate writers such as you who are not afraid to say how they believe. Always go after your heart.

tinyurl.com

I've been exploring for a little for any high quality articles or blog posts on this kind of house . Exploring in Yahoo I eventually stumbled upon this web site. Studying this info So i'm satisfied to exhibit that I have an incredibly just right uncanny feeling I discovered exactly what I needed. I such a lot for sure will make certain to do not disregard this website and provides it a look on a constant basis.

tinyurl.com

You ought to be a part of a contest for one of the most useful sites on the internet. I am going to recommend this web site!

coconut oil of

I'm impressed, I must say. Rarely do I come across a blog that's equally educative and interesting, and let me tell you, you've hit the nail on the head. The problem is something too few folks are speaking intelligently about. I'm very happy I stumbled across this during my hunt for something concerning this.

best cbd oil for pain

If some one needs expert view regarding blogging after that i advise him/her to pay a quick visit this webpage, Keep up the good job.

Mr. Fadhil Alkhouri

To: The President/CEO To: The President/CEO We are interested in your products and services for long terms procurement contracts. Please send us details of your products and services brochure if available, to enable a quick review by our international trade officer. Please contact us through the listed email below. Thank you.

Fadhil Alkhouri Almarif General Trading iqprocurefadhil@gmail.com

best cbd oil for pain

Every weekend i used to pay a visit this web site, because i wish for enjoyment, as this this web site conations actually nice funny material too.

porno

I got this site from my buddy who informed me concerning this web page and now this time I am visiting this web page and reading very informative articles here.

plenty of fish dating site

Genuinely no matter if someone doesn't understand after that its up to other people that they will help, so here it occurs.

best cbd oil for depression

Thanks a lot for sharing this with all of us you actually know what you are talking approximately! Bookmarked. Please additionally visit my website =). We can have a link change contract among us

quest bars cheap

Having read this I thought it was extremely enlightening. I appreciate you finding the time and effort to put this short article together. I once again find myself spending a lot of time both reading and commenting. But so what, it was still worth it!

SBOBET Indonesia

Hi, for all time i used to check website posts here early in the dawn, because i love to gain knowledge of more and more.

side hustle

Tremendous things here. I am very satisfied to peer your article. Thanks so much and I'm looking forward to touch you. Will you please drop me a mail?

quest bars cheap coupon twitter

Great items from you, man. I have be aware your stuff previous to and you are just extremely magnificent. I really like what you have received right here, really like what you're saying and the best way through which you are saying it. You are making it entertaining and you continue to take care of to keep it smart. I cant wait to learn much more from you. That is really a tremendous website.

ps4 games

Hey this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I'm starting a blog soon but have no coding experience so I wanted to get advice from someone with experience. Any help would be enormously appreciated!

phnom penh to ho chi minh tour

First off I want to say excellent blog! I had a quick question in which I'd like to ask if you do not mind. I was interested to know how you center yourself and clear your mind before writing. I have had a difficult time clearing my mind in getting my ideas out there. I truly do take pleasure in writing however it just seems like the first 10 to 15 minutes tend to be lost simply just trying to figure out how to begin. Any suggestions or hints? Thanks!

ps4 games

I know this website gives quality dependent content and additional information, is there any other site which offers these information in quality?

ps4 games

With havin so much content do you ever run into any issues of plagorism or copyright infringement? My blog has a lot of exclusive content I've either written myself or outsourced but it appears a lot of it is popping it up all over the web without my permission. Do you know any ways to help stop content from being ripped off? I'd definitely appreciate it.

ps4 games

It's going to be finish of mine day, except before finish I am reading this great piece of writing to increase my experience.

Jelle De Loecker