Delay.js
5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Delay.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* Utility class for working with delayed actions like setTimeout.
*
* @class tinymce.util.Delay
*/
define("tinymce/util/Delay", [
"tinymce/util/Promise"
], function(Promise) {
var requestAnimationFramePromise;
function requestAnimationFrame(callback, element) {
var i, requestAnimationFrameFunc = window.requestAnimationFrame, vendors = ['ms', 'moz', 'webkit'];
function featurefill(callback) {
window.setTimeout(callback, 0);
}
for (i = 0; i < vendors.length && !requestAnimationFrameFunc; i++) {
requestAnimationFrameFunc = window[vendors[i] + 'RequestAnimationFrame'];
}
if (!requestAnimationFrameFunc) {
requestAnimationFrameFunc = featurefill;
}
requestAnimationFrameFunc(callback, element);
}
function wrappedSetTimeout(callback, time) {
if (typeof time != 'number') {
time = 0;
}
return setTimeout(callback, time);
}
function wrappedSetInterval(callback, time) {
if (typeof time != 'number') {
time = 1; // IE 8 needs it to be > 0
}
return setInterval(callback, time);
}
function wrappedClearTimeout(id) {
return clearTimeout(id);
}
function wrappedClearInterval(id) {
return clearInterval(id);
}
return {
/**
* Requests an animation frame and fallbacks to a timeout on older browsers.
*
* @method requestAnimationFrame
* @param {function} callback Callback to execute when a new frame is available.
* @param {DOMElement} element Optional element to scope it to.
*/
requestAnimationFrame: function(callback, element) {
if (requestAnimationFramePromise) {
requestAnimationFramePromise.then(callback);
return;
}
requestAnimationFramePromise = new Promise(function(resolve) {
if (!element) {
element = document.body;
}
requestAnimationFrame(resolve, element);
}).then(callback);
},
/**
* Sets a timer in ms and executes the specified callback when the timer runs out.
*
* @method setTimeout
* @param {function} callback Callback to execute when timer runs out.
* @param {Number} time Optional time to wait before the callback is executed, defaults to 0.
* @return {Number} Timeout id number.
*/
setTimeout: wrappedSetTimeout,
/**
* Sets an interval timer in ms and executes the specified callback at every interval of that time.
*
* @method setInterval
* @param {function} callback Callback to execute when interval time runs out.
* @param {Number} time Optional time to wait before the callback is executed, defaults to 0.
* @return {Number} Timeout id number.
*/
setInterval: wrappedSetInterval,
/**
* Sets an editor timeout it's similar to setTimeout except that it checks if the editor instance is
* still alive when the callback gets executed.
*
* @method setEditorTimeout
* @param {tinymce.Editor} editor Editor instance to check the removed state on.
* @param {function} callback Callback to execute when timer runs out.
* @param {Number} time Optional time to wait before the callback is executed, defaults to 0.
* @return {Number} Timeout id number.
*/
setEditorTimeout: function(editor, callback, time) {
return wrappedSetTimeout(function() {
if (!editor.removed) {
callback();
}
}, time);
},
/**
* Sets an interval timer it's similar to setInterval except that it checks if the editor instance is
* still alive when the callback gets executed.
*
* @method setEditorInterval
* @param {function} callback Callback to execute when interval time runs out.
* @param {Number} time Optional time to wait before the callback is executed, defaults to 0.
* @return {Number} Timeout id number.
*/
setEditorInterval: function(editor, callback, time) {
var timer;
timer = wrappedSetInterval(function() {
if (!editor.removed) {
callback();
} else {
clearInterval(timer);
}
}, time);
return timer;
},
/**
* Creates throttled callback function that only gets executed once within the specified time.
*
* @method throttle
* @param {function} callback Callback to execute when timer finishes.
* @param {Number} time Optional time to wait before the callback is executed, defaults to 0.
* @return {Function} Throttled function callback.
*/
throttle: function(callback, time) {
var timer, func;
func = function() {
var args = arguments;
clearTimeout(timer);
timer = wrappedSetTimeout(function() {
callback.apply(this, args);
}, time);
};
func.stop = function() {
clearTimeout(timer);
};
return func;
},
/**
* Clears an interval timer so it won't execute.
*
* @method clearInterval
* @param {Number} Interval timer id number.
*/
clearInterval: wrappedClearInterval,
/**
* Clears an timeout timer so it won't execute.
*
* @method clearTimeout
* @param {Number} Timeout timer id number.
*/
clearTimeout: wrappedClearTimeout
};
});