How to use JavaScript to implement precise setTimeout and setInterval
How to use JavaScript to implement precise setTimeout and setInterval
如何使用 JavaScript 实现精确的 setTimeout 和 setInterval

setTimeoutPreciseSimulator & setIntervalPreciseSimulator
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-07-0
* @modified
*
* @description 使用 JavaScript 实现精确的 setTimeout 和 setInterval
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
*/
const log = console.log;
// 同步: 使用 js 实现精确的 setTimeout
function setTimeoutPreciseSimulator(callback, time = 0) {
const begin = new Date().getTime();
while (true) {
const end = new Date().getTime();
if(end - begin >= time) {
log(`end - begin`, end - begin);
callback();
break;
}
}
}
// 同步: 使用 js 实现精确的 setInterval
function setIntervalPreciseSimulator(callback, time = 0, count = 10) {
function interval(callback, time) {
const begin = new Date().getTime();
while (true) {
const end = new Date().getTime();
if(end - begin >= time) {
log(`end - begin`, end - begin);
callback();
break;
}
}
if(count) {
log(`\ncount =`, count);
count--;
interval(callback, time);
}
}
// init
interval(callback, time);
}
// setTimeoutPreciseSimulator(() => console.log(`OK 1000`), 1000)
// end - begin 1000
// OK 1000
setIntervalPreciseSimulator(() => console.log(`OK 1000`), 1000)
// end - begin 1000
// OK 1000
// count = 10
// end - begin 1000
// OK 1000
// count = 9
// end - begin 1000
// OK 1000
// count = 8
// end - begin 1000
// OK 1000
// count = 7
// end - begin 1000
// OK 1000
// count = 6
// end - begin 1000
// OK 1000
// count = 5
// end - begin 1000
// OK 1000
// count = 4
// end - begin 1000
// OK 1000
// count = 3
// end - begin 1000
// OK 1000
// count = 2
// end - begin 1000
// OK 1000
// count = 1
// end - begin 1000
// OK 1000
requestAnimationFrame bug
requestAnimationFrame bug ? can not pass params
// bug ? can not pass params
function requestAnimationFramePreciseSimulator(callback, time = 0, count = 10) {
// function interval(callback, time) {
function interval() {
const begin = new Date().getTime();
while (true) {
const end = new Date().getTime();
if(end - begin >= time) {
log(`end - begin`, end - begin);
callback();
break;
}
}
if(count) {
log(`\ncount =`, count);
count--;
requestAnimationFrame(interval);
// window.requestAnimationFrame(interval);
}
}
// init
requestAnimationFrame(interval);
// window.requestAnimationFrame(interval);
// ReferenceError: requestAnimationFrame is not defined
}
// requestAnimationFramePreciseSimulator(() => console.log(`OK 1000`), 1000)
window.requestAnimationFrame(callback);
// pass 参数???
window.requestAnimationFrame(callback(args));
window.requestAnimationFrame(callback, args);
https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame
https://caniuse.com/#feat=requestanimationframe
requestAnimationFrame
function repeatOften() {
// do whatever
requestAnimationFrame(repeatOften);
}
requestAnimationFrame(repeatOften);
https://css-tricks.com/using-requestanimationframe/
refs
https://www.cnblogs.com/xgqfrms/p/11399442.html
https://javascript.info/settimeout-setinterval
https://stackoverflow.com/questions/29971898/how-to-create-an-accurate-timer-in-javascript
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
How to use JavaScript to implement precise setTimeout and setInterval的更多相关文章
- Javascript异步编程之setTimeout与setInterval详解分析(一)
Javascript异步编程之setTimeout与setInterval 在谈到异步编程时,本人最主要会从以下三个方面来总结异步编程(注意:特别解释:是总结,本人也是菜鸟,所以总结不好的,请各位大牛 ...
- 【转】Javascript异步编程之setTimeout与setInterval
Javascript异步编程之setTimeout与setInterval 转自:http://www.tuicool.com/articles/Ebueua 在谈到异步编程时,本人最主要会从以下三个 ...
- Javascript定时器(二)——setTimeout与setInterval
一.解释说明 1.概述 setTimeout:在指定的延迟时间之后调用一个函数或者执行一个代码片段 setInterval:周期性地调用一个函数(function)或者执行一段代码. 2.语法 set ...
- [记录] javascript 对象中使用setTimeout
参考:Javascript对象中关于setTimeout和setInterval的this介绍 使用最后一个方法终于弄好了,简直了,在对象中使用setTimeout原来是这样的 做的是分钟倒计时,倒数 ...
- 《无所不能的JavaScript编程系列:setTimeout 简笔》
前言:问题引出 JavaScript中会经常用到setTimeout来推迟一个函数的执行,如: setTimeout(function(){alert("Hello World") ...
- setTimeout和setInterval区别
setTimeout和setInterval这两个函数, 大家肯定都不陌生, 但可能并不是每个用过这两个方法的同学, 都了解其内部的实质 甚至可能会错误的把两个实现定时调用的函数理解成了类似threa ...
- 深入理解setTimeout和setinterval
以前一直以为这两个函数就是简单了认为类似thread一样的东西, 认为会在一个时间片内, 并发的执行调用的函数, 似乎很好很强大, 但其实并不是如此, 实际的情况是javascript都是以单线程的方 ...
- setTimeout()和setInterval()方法的区别
setTimeout(); //5秒后执行yourFunction(),只执行一次 setInterval(); //每隔5秒执行一次 1.setTimeout(funhander,time)的作用是 ...
- Javascript的setTimeOut()和setInterval()的定时器用法
Javascript用来处理延时和定时任务的setTimeOut和setInterval函数应用非常广泛,它们都用来处理延时和定时任务,比如打开网页一段时间后弹出一个登录框,页面每隔一段时间发送异步请 ...
随机推荐
- java虚拟机——轻松搞懂jvm
一.JVM体系结构概述 JVM位置 JVM体系结构 1.1 类加载器 ClassLoader 类加载器(ClassLoader)负责加载class文件,class文件在文件开头有特定的文件标示,并 ...
- POJ1195 二维线段树
Mobile phones POJ - 1195 Suppose that the fourth generation mobile phone base stations in the Tamper ...
- vue3.0初尝试
- Java,面试题,简历,Linux,大数据,常用开发工具类,API文档,电子书,各种思维导图资源,百度网盘资源,BBS论坛系统 ERP管理系统 OA办公自动化管理系统 车辆管理系统 各种后台管理系统
Java,面试题,简历,Linux,大数据,常用开发工具类,API文档,电子书,各种思维导图资源,百度网盘资源BBS论坛系统 ERP管理系统 OA办公自动化管理系统 车辆管理系统 家庭理财系统 各种后 ...
- shell循环字符串数组
#!/bin/bash arr=("0" "1" "2" "3" "4" "5" ...
- 虚拟局域网(VLAN)__语音VLAN
1.语音VLAN特性使得访问端口能够携带来自IP电话的IP语音流量.当交换机连接到Cisco IP电话时,IP电话就用第3层IP优先级(precedence)和第2层服务级别(class of ser ...
- MIT 6.S081 聊聊xv6中的文件系统(上)
前言 Lab一做一晚上,blog一写能写两天,比做Lab的时间还长( 这篇博文是半夜才写完的,本来打算写完后立刻发出来,但由于今天发现白天发博点击量会高点,就睡了一觉后才发(几十的点击量也是点击量啊T ...
- 基于Servlet体系的HTTP请求代理转发Spring Boot组件
背景概述 两个项目组原本都是各自负责两个产品线(产品A.产品B),由于公司业务的发展,目前需要将两个产品合并成一个大产品(功能整合,部分做取舍,最终产出产品C),前后端代码必然也需要整合,包括两个产品 ...
- Kwp2000协议的应用(程序后续篇)
作者:良知犹存 转载授权以及围观:欢迎添加微信:becom_me 总述 接上篇文章,本篇继续对基于PID解析数据,如何依据J1979的标准进行解析数据 先给昨天的文章补上一张故障码对照表,昨天分析了如 ...
- Inceptor常用SQL
1.创建数据库 建一个数据库exchange_platform. DROP DATABASE IF EXISTS exchange_platform CASCADE; CREATE DATABASE ...