简体   繁体   English

如何使用setTimeout()调用jQuery(document).ready之外的函数?

[英]How to call function outside of jQuery(document).ready with setTimeout()?

My code looks something like: 我的代码如下所示:

$(document).ready(function(){
    var cont = 0;

    function func1(cont)
    {
        //Some code here
        search.setSearchCompleteCallback(this, searchComplete, null);
        //Some other code
    }
    func1(cont);

    function searchComplete()
    {
        //Some code
        cont += 1;
    if (cont < length ) {
    func1(cont);
    } else {
            // Other code
    }
    }
});

So what I want to do is delay the execution of func1(cont); 所以我想做的就是延迟func1(cont);的执行。 inside of the searchComplete() function. 在searchComplete()函数内部。 The reason for this is that all the code does is to work with the Google search API and PageRank checks and I need to slow down the script so that I won't get banned. 这样做的原因是所有代码都可以与Google搜索API和PageRank检查一起使用,并且我需要放慢脚本的速度,以免被禁止。 (Especially for the requests it makes regarding the PR check). (尤其是针对PR检查的请求)。 If I simply use setTimeout() on func1(cont); 如果我只是在func1(cont)上使用setTimeout(); it says there is no func1() defined, if I try to get the function outside $(document).ready() it sees the function but the Google code won't for for it needs the page completely loaded. 它说没有定义func1(),如果我尝试将功能移到$(document).ready()之外,它将看到该功能,但Google代码不会,因为它需要完全加载页面。

How can I fix setTimeout or how can I pause the script for a number of seconds ? 如何修复setTimeout或如何将脚本暂停几秒钟?

Thanks! 谢谢!

Write

func1(cont);

as

window.setTimeout(function() {
    func1(cont);
}, 1000);

Instead of declaring the function like this: 而不是像这样声明函数:

function func1(cont) {}

declare it like this: 这样声明:

var func1 = function(cont) {}

You'll need to rearrange your code a little: 您需要稍微重新排列代码:

$(document).ready(function(){
    var cont = 0;
    var func1;

    var searchComplete = function()
    {
        //Some code
        cont += 1;
        if (cont < length ) {
            func1(cont);
        } else {
                // Other code
        }
    }

    func1 = function(cont)
    {
        //Some code here
        search.setSearchCompleteCallback(this, searchComplete, null);
        //Some other code
    }

    func1(cont);
});

I'd try something like this. 我会尝试这样的事情。 I prefer to declare the vars and functions inside the jquery namespace, but you could equally move the cont variable and the functions outside of the document ready function and have them available globally. 我更喜欢在jQuery名称空间中声明vars和函数,但是您可以将cont变量和函数同样地移到文档就绪函数之外,并使它们全局可用。

$(document).ready(function(){
    $.cont = 0;
    $.func1 = function() {
        //Some code here
        search.setSearchCompleteCallback(this, $.searchComplete, null);
        //Some other code
    }

    $.searchComplete = function() {
        //Some code
        $.cont += 1;
        if (cont < length ) {
            setTimeout($.func1,1000);
        } else {
            // Other code
        }
    }

     setTimeout($.func1,1000); // delay the initial start by 1 second
});

Hopefully I've got your description correct: 希望我对您的描述正确:

  • document.ready() event fires 触发document.ready()事件
  • Inside document.ready() you want a function to be called after X milliseconds 在document.ready()内部,您希望在X毫秒后调用函数
  • This function wires up the Google object search.setSearchCompleteCallback() to another function (which it looks like it needs a parent object from the this ) 此函数将Google对象search.setSearchCompleteCallback()连接到另一个函数(看起来需要this的父对象)

If this is the case, why do you need any of the functions declared inside the document.ready() scope? 如果是这种情况,为什么需要在document.ready()范围内声明的任何函数? Can you't simply make all 3 global? 您不能简单地使这3个全局化吗? eg 例如

var search = null; // initialise the google object
var cont = 0;

function timedSearch()
{
  search.setSearchCompleteCallback(this, searchComplete, null);
}

function searchComplete()
{
   if (++cont < length) // postfix it below if this is wrong
       setTimeout(timedSearch,1000);
}

$(document).ready(function()
{
   setTimeout(timedSearch,1000);
}

Hit me with the downvotes if I've misunderstood. 如果我误会了我的话,请打倒我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM