Question

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 completely loaded.

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

Thanks!

Was it helpful?

Solution

Write

func1(cont);

as

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top