ALF
ALF

Reputation: 47

Javascript animation WITHOUT jquery/settimeout/setinterval

As the title says.. is there any possible way to have the simplest of animations in javascript without the use of Jquery, settimeout, and setinterval. I've been researching for too long now and cannot find anything. There may not be a way to do it, just wondering. Looking for something cross browser as well, so CSS animations won't work either.

Upvotes: 0

Views: 1346

Answers (4)

user578895
user578895

Reputation:

The "correct" way to do most animations now is with window.requestAnimationFrame, which acts like a setTimeout in that it runs a callback function at a specified time.

It's optimized to run at 60fps, and in some browsers (FF) it will give you the exact number of milliseconds until the next draw so that you can very precisely calculate animation positions.

Google "requestAnimationFrame shim" for various cross-browser implementations for older browsers. The shims all use setTimeout — there's no way around that.

Upvotes: 5

epascarello
epascarello

Reputation: 207531

No, how can you do it without intervals/timeouts and be able to do it cross browser? You need something to keep triggering it.

Upvotes: 1

Hawken
Hawken

Reputation: 2119

No, I don't think there is any way of making a pause in javascript without using setTimeout() or setInterval().

The only option I can think of would be to have javascript wait for some event like an XMLHttpRequest object response, which is beyond ridiculous.

If you're looking for some convenient "pause execution for (x) milliseconds and continue" like Thread.sleep() in JavaScript or usleep() on Unix systems, there is no such thing sadly.

I remember hearing that for timing purposes on a Ruby script, running in Google Sketchup, someone went to the trouble of making an invisible HTML window just to run a little JavaScript using setInterval() so they would have a timing mechanism. I think if you go to such lengths to run JavaScript and still use setInterval() there probably is no other option.

Upvotes: 0

zzzzBov
zzzzBov

Reputation: 179096

Not unless you're animating something on user input. If you animate something when the user triggers mouse move events, or page scroll events, it could work, but it would be stop-and-go, and 100% dependent on the user triggering it.

Upvotes: 1

Related Questions