Skip to content

Commit 576c70b

Browse files
author
dntzhang
committed
1 parent 0f01e65 commit 576c70b

File tree

5 files changed

+103
-65
lines changed

5 files changed

+103
-65
lines changed

.gitattributes

-22
This file was deleted.

.gitignore

-43
This file was deleted.

index.html

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>raf-interval</title>
6+
</head>
7+
<body>
8+
<script src="raf-interval.js"></script>
9+
<script>
10+
var i = 0
11+
var rafInterval = setRafInterval(function() {
12+
console.log(i++)
13+
if (i > 6) {
14+
clearRafInterval(rafInterval)
15+
}
16+
},1000)
17+
18+
var j = 0
19+
var interval = setInterval(function() {
20+
console.log(j++)
21+
if (j > 6) {
22+
clearInterval(interval)
23+
}
24+
},1000)
25+
26+
</script>
27+
</body>
28+
</html>

package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "raf-interval",
3+
"version": "0.1.0",
4+
"description": "setRafInterval and clearRafInterval with requestAnimationFrame.",
5+
"scripts": {
6+
},
7+
"dependencies": {
8+
},
9+
"devDependencies": {
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/AlloyTeam/raf-interval.git"
14+
},
15+
"keywords": [
16+
"pasition",
17+
"path",
18+
"svg",
19+
"canvas"
20+
],
21+
"author": "dntzhang",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/AlloyTeam/raf-interval/issues/new"
25+
},
26+
"homepage": "http://alloyteam.github.io/raf-interval"
27+
}

raf-interval.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Created by dntzhang on 2017/7/13.
3+
*/
4+
5+
;(function() {
6+
7+
var queue = [],
8+
id = -1,
9+
ticking = false,
10+
tickId = null
11+
12+
window.setRafInterval = function (fn, interval) {
13+
id++
14+
queue.push({id: id, fn: fn, interval: interval, lastTime: new Date()})
15+
if (!ticking) {
16+
var tick = function () {
17+
tickId = requestAnimationFrame(tick)
18+
queue.forEach(function (item) {
19+
if (item.interval < 17 || new Date - item.lastTime >= item.interval) {
20+
item.fn()
21+
item.lastTime = new Date()
22+
}
23+
})
24+
}
25+
ticking = true
26+
tick()
27+
}
28+
return id
29+
}
30+
31+
window.clearRafInterval = function (id) {
32+
var i = 0,
33+
len = queue.length
34+
35+
36+
for (; i < len; i++) {
37+
if (id === queue[i].id) {
38+
queue.splice(i, 1)
39+
break
40+
}
41+
}
42+
43+
if (queue.length === 0) {
44+
cancelAnimationFrame(tickId)
45+
ticking = false
46+
}
47+
}
48+
})();

0 commit comments

Comments
 (0)