js 用requestAnimationFrame api实现30s倒计时
时间: 2023-11-09 10:03:18 浏览: 233
js实现自动倒计时按钮30秒后可用.zip
可以使用以下代码实现30秒倒计时:
```javascript
let countDown = 30;
let startTime = null;
function animate(timestamp) {
if (!startTime) startTime = timestamp;
const elapsedTime = timestamp - startTime;
const seconds = Math.floor((countDown * 1000 - elapsedTime) / 1000);
console.log(seconds);
if (elapsedTime < countDown * 1000) {
requestAnimationFrame(animate);
}
}
requestAnimationFrame(animate);
```
阅读全文