setTimeOut函数传参数

这样使用,后面的4000无效

setTimeout(removeGift(customer_id,gift_id),4000);

function removeGift(customer_id,gift_id) {

        var x = 100;
        var y = 900;
        var rand = parseInt(Math.random() * (x - y + 1) + y); // 随机的位置
        $("#gift_"+customer_id+"_"+gift_id).animate({
            bottom:900,
            opacity:"0",
            left: 1000,
        },1000,'',function () {
            $("#gift_"+customer_id+"_"+gift_id).remove();
        });           
}

这样使用,后面的4000秒有效

setTimeout(removeGift(customer_id,gift_id),4000);

function removeGift(customer_id,gift_id) {
        return function(){
            var x = 100;
            var y = 900;
            var rand = parseInt(Math.random() * (x - y + 1) + y); // 随机的位置
            $("#gift_"+customer_id+"_"+gift_id).animate({
                bottom:900,
                opacity:"0",
                left: 1000,
            },1000,'',function () {
                $("#gift_"+customer_id+"_"+gift_id).remove();
            });
        }
}

通过return function,就有效果。很奇怪!

posted @ 2017-03-30 18:04  TBHacker  阅读(863)  评论(0编辑  收藏  举报