Skip to content

Instantly share code, notes, and snippets.

@lazyTai
Created September 24, 2017 01:44
Show Gist options
  • Save lazyTai/0b1c586f2aa85f8566ece13ccb9bf284 to your computer and use it in GitHub Desktop.
Save lazyTai/0b1c586f2aa85f8566ece13ccb9bf284 to your computer and use it in GitHub Desktop.
requestAnimationFrame‘
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>requestAnimationFrame</title>
</head>
<body>
<style>
.bxo {
position: absolute;
width: 100px;
height: 100px;
background: #eee;
}
</style>
<div class="bxo"></div>
<script>
var cc = console;
/*all is 兼容 all the chrome*/
window.requestAnimationFrame = (function () {
return window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame || function (/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
window.setTimeout(callback, 1000 / 60);
};
})()
var x = 0;
var callback = function () {
cc.log(x)
if (x >= 100) {
return false
}
x++;
window.requestAnimationFrame(callback)
}
window.requestAnimationFrame(callback)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment