-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequestanimationframe运用.html
More file actions
75 lines (69 loc) · 1.94 KB
/
Copy pathrequestanimationframe运用.html
File metadata and controls
75 lines (69 loc) · 1.94 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="J_num" style="margin: 50px; width: 200px; height: 200px; display: inline-block; background: #ccc"></div>
<div id="J_num2" style="margin: 50px; width: 200px; height: 200px; display: inline-block; background: #ccc"></div>
<script src="http://libs.useso.com/js/jquery/1.9.1/jquery.min.js"></script>
<script>
// requestAnim shim layer by Paul Irish
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
//未优化状态
(function(){
var timer;
var $txt = $('#J_num'),
num = 0;
function play(){
//定时器
timer = setTimeout(function(){
stop();
next();
},1000)
}
function stop(){
clearTimeout(timer)
}
function next(){
$txt.text(num++);
play();
}
play();
})();
//使用raf优化后
(function(){
var timer;
var $txt = $('#J_num2'),
num = 0;
function play(){
//定时器
timer = setTimeout(function(){
requestAnimationFrame(function(){
stop();
next();
});
},1000)
}
function stop(){
clearTimeout(timer)
}
function next(){
$txt.text(num++);
play();
}
play();
})();
</script>
</body>
</html>