-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatchstop.js
More file actions
29 lines (29 loc) · 735 Bytes
/
watchstop.js
File metadata and controls
29 lines (29 loc) · 735 Bytes
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
(function(global) {
var getTime = function() {
return Date.now();
};
var WatchStop = {
fps: function(func, fpsNum) {
var canContinue = true,
frameDuration = 1000 / fpsNum,
initFrame = Math.floor(getTime() / frameDuration);
var cancel = function() {
canContinue = false;
};
var info = {
last: cancel,
frame: 0
};
var functionCaller = function() {
func(info);
if(canContinue) {
var curTime = getTime(), curFrame = Math.floor(curTime / frameDuration) + 1;
info.frame = curFrame - initFrame;
setTimeout(functionCaller, curFrame * frameDuration - curTime);
}
};
functionCaller();
}
};
global.WatchStop = (global.module || {}).exports = WatchStop;
})(this);