• 定时器setTimeout()的传参方法


    更具体的代码:http://www.cnblogs.com/3body/p/5416830.html

    // 由于setTimeout()的延迟执行特性,所以在执行的函数中直接使用外部函数的变量是无法获取到的,因为当执行的时候变量已经销毁了。所以,这里就使用了一个闭包的方法来达到能调用外部函数的目的。
    // 通常的setTimeout(function(){...},100)是这样写的,也就是第一个参数是个函数,里面是要执行的代码片段。
    // 这里就使用了闭包的方法return function(){...},第一个参数则接收到了一个函数,并往闭包中传入参数,这样就能先将变量赋给闭包的形参,里面的函数调用的就是闭包的形参,外部函数的销毁便不会影响到函数的执行了
    addUl=setTimeout(function(a,id,width){
        return function(){
            $.post(
            '<{:U("Index/type")}>',
            {'id':id},
            function(data){
                if (data) {
                    var ul='<ul style="background:white;position:absolute;left:'+width+'px;top:-1px;z-index:100;border:1px solid #6386ae";>';
                    var id;
                    var href;
                    $.each(data,function(n,v){
                        id=v['type_id'];
                        // 不能使用array()传参,并且还要加上Home
                        href='<{:U("Home/Pro/more/type/'+id+'")}>';
                        ul+='<li style="margin:0;padding:0;text-align:center;100px;height:40px;overflow:hidden;" title="'+v['type_name']+'"><a href="'+href+'" style="margin:0;auto;">'+v['type_name']+'</a></li>';
                    });
                    ul+='</ul>';
                    a.after(ul);
                };
            }
            );
        }
    }(a,id,width),200);
    
    
    
    // 第二种传参方法:第二个参数之后的参数就是传的参数
    setTimeout(function(num){
        alert(num);  //弹出123
    },1000,123);
  • 相关阅读:
    图书管理系统登录界面
    图书管理系统的管理者界面
    图书管理系统-servlet层
    图书管理系统的dao层
    Linux 内核优化
    第十一章 Nginx之服务代理
    第十章 Nginx之LNMP拆分
    第九章 Nginx之LNMP架构
    第八章 Nginx常用模块
    第七章 WEB服务之Nginx
  • 原文地址:https://www.cnblogs.com/3body/p/5416834.html
Copyright © 2020-2023  润新知