raf.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { inBrowser } from './env'
  2. const DEFAULT_INTERVAL = 100 / 60
  3. function noop() {
  4. }
  5. export const requestAnimationFrame = (() => {
  6. /* istanbul ignore next */
  7. if (!inBrowser) {
  8. return noop
  9. }
  10. return window.requestAnimationFrame ||
  11. /* istanbul ignore next */
  12. window.webkitRequestAnimationFrame ||
  13. /* istanbul ignore next */
  14. window.mozRequestAnimationFrame ||
  15. /* istanbul ignore next */
  16. window.oRequestAnimationFrame ||
  17. // if all else fails, use setTimeout
  18. /* istanbul ignore next */
  19. function (callback) {
  20. return window.setTimeout(callback, (callback.interval || DEFAULT_INTERVAL) / 2) // make interval as precise as possible.
  21. }
  22. })()
  23. export const cancelAnimationFrame = (() => {
  24. /* istanbul ignore next */
  25. if (!inBrowser) {
  26. return noop
  27. }
  28. return window.cancelAnimationFrame ||
  29. /* istanbul ignore next */
  30. window.webkitCancelAnimationFrame ||
  31. /* istanbul ignore next */
  32. window.mozCancelAnimationFrame ||
  33. /* istanbul ignore next */
  34. window.oCancelAnimationFrame ||
  35. /* istanbul ignore next */
  36. function (id) {
  37. window.clearTimeout(id)
  38. }
  39. })()