Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript setTimeout立即运行_Javascript_Jquery_Web - Fatal编程技术网

Javascript setTimeout立即运行

Javascript setTimeout立即运行,javascript,jquery,web,Javascript,Jquery,Web,我的页面上有一些元素,我想通过JQuery扩展函数调用它们上面的函数。我有此功能声明: (function ($) { $.fn.enable = function (delay) { console.log(delay); //logs 3000 setTimeout(function (elem) { console.log(elem); elem.css("opa

我的页面上有一些元素,我想通过JQuery扩展函数调用它们上面的函数。我有此功能声明:

(function ($) {
        $.fn.enable = function (delay) {
            console.log(delay); //logs 3000
            setTimeout(function (elem) {
                console.log(elem);
                elem.css("opacity", "1");
            }(this), delay);
            return this;
        };
    })(jQuery);
如您所知,它在JQuery对象上声明了一个
enable
函数。现在,当我把这样的事情称为:

$("#start").enable(3000);
函数
enable
运行,但是
函数(elem).
中的代码立即运行,而不是在一段时间后运行


如何以及为什么???

这是因为您正在调用函数“function(elem)”,而不是将其作为参数提供。试试这个

(function ($) {
        $.fn.enable = function (delay) {
            console.log(delay); //logs 3000
             var elem = this;
            setTimeout(function () {
                console.log(elem);
                elem.css("opacity", "1");
            }, delay);  //you should not call a function here
            return this;
        };
    })(jQuery);

这是因为您正在调用函数“function(elem)”,而不是将其作为参数提供。试试这个

(function ($) {
        $.fn.enable = function (delay) {
            console.log(delay); //logs 3000
             var elem = this;
            setTimeout(function () {
                console.log(elem);
                elem.css("opacity", "1");
            }, delay);  //you should not call a function here
            return this;
        };
    })(jQuery);

这是因为您正在调用函数“function(elem)”,而不是将其作为参数提供。试试这个

(function ($) {
        $.fn.enable = function (delay) {
            console.log(delay); //logs 3000
             var elem = this;
            setTimeout(function () {
                console.log(elem);
                elem.css("opacity", "1");
            }, delay);  //you should not call a function here
            return this;
        };
    })(jQuery);

这是因为您正在调用函数“function(elem)”,而不是将其作为参数提供。试试这个

(function ($) {
        $.fn.enable = function (delay) {
            console.log(delay); //logs 3000
             var elem = this;
            setTimeout(function () {
                console.log(elem);
                elem.css("opacity", "1");
            }, delay);  //you should not call a function here
            return this;
        };
    })(jQuery);

如果以
(this)
结束匿名函数,则传递的是它的返回值,而不是函数。如果以
(this)
结束匿名函数,则传递的是它的返回值,而不是函数。如果以
(this)
结束匿名函数,则传递的是它的返回值,不是函数。如果以
(this)
结束匿名函数,则传递的是它的返回值,而不是函数。