zoukankan      html  css  js  c++  java
  • setTimeout

        for (var i = 0; i < 5; i++) {
            setTimeout(function () {
                console.log(new Date, i);
            }, 1000);
        }
        console.log(new Date, i);
    
        // Mon Apr 20 2020 10: 32: 00 GMT + 0800(中国标准时间) 5
        // Mon Apr 20 2020 10: 32: 01 GMT + 0800(中国标准时间) 5
        // Mon Apr 20 2020 10: 32: 01 GMT + 0800(中国标准时间) 5
        // Mon Apr 20 2020 10: 32: 01 GMT + 0800(中国标准时间) 5
        // Mon Apr 20 2020 10: 32: 01 GMT + 0800(中国标准时间) 5
        // Mon Apr 20 2020 10: 32: 01 GMT + 0800(中国标准时间) 5

    二:

        1 setTimeout的自参数
        for (var i = 0; i < 5; i++) {
            setTimeout(function (j) {
                console.log(new Date, j);
            }, 1000, i);
        }
        console.log(new Date, i);
    
    
        2闭包写法
        for (var i = 0; i < 5; i++) {
            (function (j) {
                setTimeout(function (i) {
                    console.log(new Date, j);
                }, 1000);
            })(i)
    
        }
        console.log(new Date, i);
    
    
    
    
        3函数体
        var foo = function (i) {
            setTimeout(function (i) {
                console.log(new Date, j);
            }, 1000);
        }
        for (var i = 0; i < 5; i++) {
            foo(i)
        }
        console.log(new Date, i);
    
    
        // Mon Apr 20 2020 10: 44: 22 GMT + 0800(中国标准时间) 5
        // Mon Apr 20 2020 10: 44: 23 GMT + 0800(中国标准时间) 0
        // Mon Apr 20 2020 10: 44: 23 GMT + 0800(中国标准时间) 1
        // Mon Apr 20 2020 10: 44: 23 GMT + 0800(中国标准时间) 2
        // Mon Apr 20 2020 10: 44: 23 GMT + 0800(中国标准时间) 3
        // Mon Apr 20 2020 10: 44: 23 GMT + 0800(中国标准时间) 4
  • 相关阅读:
    后台架构设计—数据存储层
    Linux应用程序基础
    Linux文件管理命令笔记
    CentOS7搭建LAMP实战
    (ospf、rip、isis、EIGRP)常见的动态路由协议简介
    python while 循环语句
    获取linux帮助命令
    破解linux虚拟机的密码
    gawk编程语言
    MySQL触发器在PHP项目中用来做信息备份、恢复和清空的方法介绍
  • 原文地址:https://www.cnblogs.com/gaoht/p/12736403.html
Copyright © 2011-2022 走看看