在javascript for循环sleep

简介: 在javascript for循环sleep

直接上代码吧

var interval = 1 * 1000; // 10 seconds;
for(let i= 0 ;i<1000;i++){
    setTimeout( function (i) {
        console.info(new Date().pattern("yyyy-MM-dd hh:mm:ss"))
    }, interval * i, i);
}
Date.prototype.pattern=function(fmt) {
    var o = {
        "M+" : this.getMonth()+1, //月份
        "d+" : this.getDate(), //日
        "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
        "H+" : this.getHours(), //小时
        "m+" : this.getMinutes(), //分
        "s+" : this.getSeconds(), //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S" : this.getMilliseconds() //毫秒
    };
    var week = {
        "0" : "/u65e5",
        "1" : "/u4e00",
        "2" : "/u4e8c",
        "3" : "/u4e09",
        "4" : "/u56db",
        "5" : "/u4e94",
        "6" : "/u516d"
    };
    if(/(y+)/.test(fmt)){
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    if(/(E+)/.test(fmt)){
        fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);
    }
    for(var k in o){
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
}
相关文章
|
6天前
|
JavaScript 前端开发 安全
JavaScript中的循环控制:while、do-while与for详解
【4月更文挑战第7天】本文探讨JavaScript的三种主要循环结构:while、do-while和for。while循环在满足条件时执行代码块,注意避免无限循环;do-while循环至少执行一次,适合先执行后判断的场景;for循环结合初始化、条件和迭代,适合遍历。理解每种循环的特点和适用场景,结合编程技巧,如使用break和continue,选择合适的循环方式,能提升代码效率和可读性。记得关注循环性能和避免不必要的计算。
21 0
|
6天前
|
JavaScript
在循环内错误使用函数定义(js的问题)
在循环内错误使用函数定义(js的问题)
13 0
|
6天前
|
JavaScript
JS使用循环求100内奇数之和
JS使用循环求100内奇数之和
18 1
|
6天前
|
JavaScript 前端开发
JS——while 循环和 do while 循环:究竟有什么区别?
JS——while 循环和 do while 循环:究竟有什么区别?
25 1
|
6天前
|
JavaScript 前端开发
【面试题】在JS循环中使用await会怎么样?
【面试题】在JS循环中使用await会怎么样?
|
4天前
|
JavaScript 前端开发
比较JavaScript中的for...in和for...of循环
比较JavaScript中的for...in和for...of循环
|
6天前
|
JavaScript 前端开发
JavaScript 循环方法详解
JavaScript 循环方法详解
22 1
|
6天前
|
JavaScript 前端开发
JavaScript 条件循环语句(for 循环)
JavaScript 条件循环语句(for 循环)
|
6天前
|
前端开发 JavaScript 开发者
遍历指南:JavaScript 中的 for、for-in、for-of 和 forEach 循环详解
遍历指南:JavaScript 中的 for、for-in、for-of 和 forEach 循环详解
22 3
|
6天前
|
JavaScript 索引
JS 几种循环遍历
JS 几种循环遍历
9 0
JS 几种循环遍历