最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call function outside of jQuery(document).ready with setTimeout()? - Stack Overflow

programmeradmin0浏览0评论

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); inside of the searchComplete() function. 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. (Especially for the requests it makes regarding the PR check). If I simply use setTimeout() on func1(cont); 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 pletely loaded.

How can I fix setTimeout or how can I pause the script for a number of seconds ?

Thanks!

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); inside of the searchComplete() function. 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. (Especially for the requests it makes regarding the PR check). If I simply use setTimeout() on func1(cont); 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 pletely loaded.

How can I fix setTimeout or how can I pause the script for a number of seconds ?

Thanks!

Share Improve this question asked Sep 3, 2009 at 20:22 BraynBrayn 1,78610 gold badges27 silver badges33 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5 +100

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.

$(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
  • Inside document.ready() you want a function to be called after X milliseconds
  • This function wires up the Google object search.setSearchCompleteCallback() to another function (which it looks like it needs a parent object from the this)

If this is the case, why do you need any of the functions declared inside the document.ready() scope? Can you't simply make all 3 global? e.g.

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.

发布评论

评论列表(0)

  1. 暂无评论