frame.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * DevExtreme (animation/frame.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var windowUtils = require("../core/utils/window");
  11. var window = windowUtils.hasWindow() ? windowUtils.getWindow() : {};
  12. var callOnce = require("../core/utils/call_once");
  13. var FRAME_ANIMATION_STEP_TIME = 1e3 / 60;
  14. var request = function(callback) {
  15. return setTimeout(callback, FRAME_ANIMATION_STEP_TIME)
  16. };
  17. var cancel = function(requestID) {
  18. clearTimeout(requestID)
  19. };
  20. var setAnimationFrameMethods = callOnce(function() {
  21. var nativeRequest = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame;
  22. var nativeCancel = window.cancelAnimationFrame || window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame || window.oCancelAnimationFrame || window.msCancelAnimationFrame;
  23. if (nativeRequest && nativeCancel) {
  24. request = nativeRequest;
  25. cancel = nativeCancel
  26. }
  27. if (nativeRequest && !nativeCancel) {
  28. var canceledRequests = {};
  29. request = function(callback) {
  30. var requestId = nativeRequest.call(window, function() {
  31. try {
  32. if (requestId in canceledRequests) {
  33. return
  34. }
  35. callback.apply(this, arguments)
  36. } finally {
  37. delete canceledRequests[requestId]
  38. }
  39. });
  40. return requestId
  41. };
  42. cancel = function(requestId) {
  43. canceledRequests[requestId] = true
  44. }
  45. }
  46. });
  47. exports.requestAnimationFrame = function() {
  48. setAnimationFrameMethods();
  49. return request.apply(window, arguments)
  50. };
  51. exports.cancelAnimationFrame = function() {
  52. setAnimationFrameMethods();
  53. cancel.apply(window, arguments)
  54. };