Intervals and Timeouts
2024-09-05 12:19:34
Intervals
var num = 0;
var max = 10; function incrementNumber(){
num++; // if the max has not been reached, set another timeout
if(num < max){
setTimeout(incrementNumber, 500);
} else {
alert("Done");
}
} setTimeout(incrementNumber, 500)
Timeouts
var num = 0;
var max = 10; function incrementNumber(){
num++; // if the max has not been reached, set another timeout
if(num < max){
setTimeout(incrementNumber, 500);
} else {
alert("Done");
}
} setTimeout(incrementNumber, 500)
Note that when you're using timeouts, it is unnecessary to track the timeoutID, because the execution will stop on its own and continue only if another timeout is set. The pattern is considered a best practice for setting intervals without actually using intervals. True intervals are rarely used in production environments because the time between the end of one interval and the beginning of the next is not necessarily guaranteed, and some intervals may be skipped. Using timeouts, as in the preceding example, ensures that can't happen. Generally speaking, it's best to avoid intervals.
最新文章
- 使用Microsoft Roslyn提取C#和VB.NET源代码中的字符串常量
- Sort Colors [LeetCode]
- leetcode 1_2_3_7
- centos 服务开机启动设置
- Android开源项目汇总【转】
- [CF353C]Find Maximum(贪心)
- c++关于接口机制和不完全类型的小问题
- Android的Intent作用
- 使用CSS修改HTML5 input placeholder颜色( 转载 )
- 文件IO 练习题
- linux操作系下RAR的使用
- linux 文件系统操作()
- devops工具-Ansible基础
- 迭代器与泛型for
- python-运算符重载
- 弱也有弱的ACM经历
- C#如何使用VS2010与SQL2008建立链接及初步调用(转)
- js 解决图片居中问题
- Hibernate的查询方式汇总
- 下拉框多选实现回显及sql