Jump to content

Any alternatives to requestAnimationFrame/setTimeout/setInterval that don't run asynchronously?


Psychho
 Share

Recommended Posts

Synchronous timing functions? Why would you want those?

Of course you can insert an old-fashioned "while (Date.now() < start + delay);" loop, but this is not a good strategy - since JavaScript code is ran in single thread, doing so will efficiently block either tab or whole browser for the time of waiting.

Gotta always run the physics simulation before/after rendering but instead its running them asynchronously creating a really bad shaking effect on moving objects.

Also I tried using a while loop close to what you described that breaks every 10 updates to call a requestAnimationFrame(so the tab doesn't freeze) it just caused random extreme lag and even worse shaking/jittering :(.

 

Jittering/shaking is normally not a problem except for when the browser keeps rendering at 30fps and is running physics at around 4fps -__-.

No clue how this is happening when my code is just

function Update() {    Physics();    Render();    requestAnimationFrame(Update);}

Video of the jittering aswell, just cause 

http://www.youtube.com/watch?v=FPJvA7NHolg

Link to comment
Share on other sites

Having code structured like

render()handle_physics()
Would have ensured that physics-related code is ran -after- rendering code. Although, provided that part of code remains synchronous, this most likely would not solve your problem.

Perhaps a setTimeout(handler, 0) would help at running code "just after" rendering.

I heavily doubt that this is the source of problem though.

Link to comment
Share on other sites

Having code structured like

render()handle_physics()
Would have ensured that physics-related code is ran -after- rendering code. Although, provided that part of code remains synchronous, this most likely would not solve your problem.

Perhaps a setTimeout(handler, 0) would help at running code "just after" rendering.

I heavily doubt that this is the source of problem though.

 

Nope didn't make a difference, also uploaded a video above of the jittering.

Just tried the code below as-well and it didn't make a difference :(

function Update() {    setTimeout(Physics,0);    Render();    requestAnimationFrame(Update);}

EDIT: Found a fix, put physics and rendering in two seperate requestAnimationFrame calls lol

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...