匿名函数内部的 setTimeout

发布于 2024-10-11 01:50:19 字数 493 浏览 10 评论 0原文

我想在 jQuery 函数中调用 editObject() ,初始化调用确实有效,但 setTimeout 不起作用,如何让它运行?控制台说 setTimeout 调用时未定义 editObject:

(function($){
    $.fn.extend({

                    ...

                    editObject()

            function editObject() {
                alert("Test!");
                setTimeout('editObject()', 1000);
            }

            return this.each(function() {
                var o = options;
            });
        }
    });
})(jQuery);

感谢帮助!

I want to call editObject() inside of my jQuery function, the initalisation call does work, but setTimeout doesn't work, how to get it running? Console says that editObject is not defined when called by setTimeout:

(function($){
    $.fn.extend({

                    ...

                    editObject()

            function editObject() {
                alert("Test!");
                setTimeout('editObject()', 1000);
            }

            return this.each(function() {
                var o = options;
            });
        }
    });
})(jQuery);

Thanks for help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

私藏温柔 2024-10-18 01:50:19

它不起作用,因为 editObject() 是在匿名函数的范围内声明的,但 setTimeout() 会评估您在全局上下文中传递给它的字符串。试试这个:

setTimeout(editObject, 1000);

It doesn't work because editObject() is declared in the scope of your anonymous function, but setTimeout() evals the string you passed it in a global context. Try this:

setTimeout(editObject, 1000);
小兔几 2024-10-18 01:50:19

你可以试试这个:

setTimeout(function(){ editObject.call() }, 1000);

或者:

setTimeout(arguments.callee, 1000);

You could try this:

setTimeout(function(){ editObject.call() }, 1000);

Or:

setTimeout(arguments.callee, 1000);
ら栖息 2024-10-18 01:50:19

您应该避免使用 setTimeout 的字符串版本。

我相信,在这种情况下,Javascript 正在寻找函数 window.editObject 而该函数不存在。

您应该使用:

setTimeout(editObject,1000);

相反,因为这是在调用时获取对对象的引用,因此当超时到期时,该函数将可供 Javascript 调用

You should avoid using the string version of setTimeout.

I believe that, in this case, Javascript is looking for the function window.editObject and this does not exist.

You should use:

setTimeout(editObject,1000);

instead, as this is grabbing a reference to the object at call-time and so the function will be available to Javascript, for calling, when the timeout expires

最终幸福 2024-10-18 01:50:19

直接使用函数引用(而不是在全局上下文中计算的字符串),如下所示:

setTimeout(editObject, 1000);

Use the function reference directly (rather than a string, which is evaluated in a global context), like this:

setTimeout(editObject, 1000);
柠栀 2024-10-18 01:50:19

更新:
我当然误解了这个问题,我的原始代码经过编辑后可以正常工作:

var foo = function () {
    var that = this;
    that.editObject = function() {
      alert('Test');
      setTimeout(that.editObject, 1000);
    }
}
new foo().editObject();

...

每当 Javascript 引擎尝试调用 jQuery 函数中定义的“editObject()”时,它就超出了 jQuery 函数的范围,它实际上位于全局作用域,但 editObject() 函数是在 jQuery 函数中定义的。

您可以在全局范围内创建 editObject 或创建对 jQuery 函数的引用并将其传递到 setTimeout 调用中,例如:

var that = this;
that.editObject = function() {
  alert('Test');
  setTimeout(that.editObject(), 1000);
}

UPDATE:
I certainly misunderstood the problem, my original code edited to be working:

var foo = function () {
    var that = this;
    that.editObject = function() {
      alert('Test');
      setTimeout(that.editObject, 1000);
    }
}
new foo().editObject();

...

Whenever the Javascript engine will try to call "editObject()" defined in your jQuery function, it is out of your jQuery function's scope, it is actually in the global scope, but your editObject() function is defined in your jQuery function.

You can create the editObject in the global scope or create a reference to your jQuery function and pass it in your setTimeout call, like:

var that = this;
that.editObject = function() {
  alert('Test');
  setTimeout(that.editObject(), 1000);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文