views:

2054

answers:

5

Hey,

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!

A: 

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);
});
Greg
The first part is not necessary, the two declarations are equivalent.
bandi
Nearly but not quite
Greg
It looks identical, and your code also does not delay the repeat call to func1(). Declaring functions in the scope of assigning them to variables in the scope is identical in terms of scoping and both will work the same.
Guss
I have tried your code but it still does not found "func1" when I try to use setTimeout in "searchComplete"
Brayn
I've edited - give it a go now
Greg
I've updated the code but I still get the "func1 is not defined" error. I use setTimeout on func1(cont) right under: "if (cont < length ) {"
Brayn
A: 

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
});
WibblePoop
I've tried this approach but it throws an error from the jQuery library itself...
Brayn
A: 

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.

Chris S
I have tried this, but then I gen an "google.search is undefined" error. I think the google API needs the page to be loaded before you do anything. In the documentation they use the google.setOnLoadCallback() function to fire "func1". I've tried to use it but it setTimeout still won't work.
Brayn
+2  A: 

Write

func1(cont);

as

window.setTimeout(function() {
    func1(cont);
}, 1000);
strager
This was it! It works now. Can you please explain what was the problem please ? Thanks!
Brayn
@Brayn, Did your original code look like this: `window.setTimeout(func1(cont), 1000);`? Or was it `window.setTimeout('func1(cont)', 1000);`? In the former case, you are calling `func1` immediately, then `setTimeout` complains it wasn't given a function argument (because `func1` doesn't return a function). In the latter case, `func1` is being called in the global scope/closure, but there is no such `func1` there, thus the error.
strager
I was using: window.setTimeout('func1(cont)', 1000);. Thanks, now I get it!
Brayn
A: