2009-09-03 84 views
1

我的代碼看起來是這樣的:如何使用setTimeout()在jQuery(document).ready之外調用函數?

$(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 
    } 
    } 
}); 

所以我想要做的就是延遲FUNC1(續)執行;在searchComplete()函數內部。原因是所有的代碼都是使用Google搜索API和PageRank檢查,我需要減慢腳本的速度,這樣我纔不會被禁止。 (特別是針對PR檢查的要求)。 如果我簡單地在func1(cont)上使用setTimeout();它表示沒有定義func1(),如果我嘗試獲取$(document).ready()之外的函數,它會看到函數,但Google代碼不會爲它需要完全加載的頁面。

如何解決setTimeout或如何暫停腳本數秒?

謝謝!

回答

5

func1(cont); 

作爲

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

就是這樣!它現在有效。你能解釋一下有什麼問題嗎?謝謝! – Brayn 2009-09-07 17:36:10

+0

@Brayn,你原來的代碼是這樣的:'window.setTimeout(func1(cont),1000);'?或者它是'window.setTimeout('func1(cont)',1000);'?在前一種情況下,您立即調用'func1',然後'setTimeout'抱怨沒有給出函數參數(因爲'func1'沒有返回函數)。在後一種情況下,'func1'在全局範圍/閉包中被調用,但是在那裏沒有'func1',因此出現了錯誤。 – strager 2009-09-07 17:47:29

+0

我使用的是:window.setTimeout('func1(cont)',1000);.謝謝,現在我明白了! – Brayn 2009-09-07 22:20:58

1

相反聲明函數像這樣的:

function func1(cont) {} 

聲明它是這樣的:

var func1 = function(cont) {} 

你需要重新安排你的代碼一點點:

$(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); 
}); 
+0

第一部分是沒有必要的,兩個聲明是等價的。 – bandi 2009-09-03 20:41:41

+1

近似但不完全 – Greg 2009-09-03 21:04:56

+0

看起來完全相同,並且您的代碼也不會延遲重複調用func1()。在範圍內將它們分配給變量的範圍內聲明函數在範圍界定方面是相同的,並且兩者的工作原理是相同的。 – Guss 2009-09-04 00:02:35

0

我'嘗試這樣的事情。我更願意在jquery命名空間中聲明變量和函數,但是您可以同時將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 
}); 
+0

我試過這種方法,但它從jQuery庫本身引發錯誤... – Brayn 2009-09-07 17:02:17

0

希望我有你的描述正確的是:

  • 的document.ready()事件觸發
  • 裏面的document.ready()你想一個函數X毫秒後調用
  • 該函數將Google對象search.setSearchCompleteCallback()連接到另一個函數(它看起來像它需要來自this的父對象)

如果是這種情況,爲什麼您需要在document.ready()作用域內聲明的任何函數?你不能簡單地把所有3個全球?例如

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); 
} 

如果我誤解了我的話,可以給我點贊。

+0

我試過這個,但後來我創「google.search未定義」錯誤。我認爲google API需要在你做任何事之前加載頁面。在文檔中,他們使用google.setOnLoadCallback()函數來激發「func1」。我試圖使用它,但它setTimeout仍然無法正常工作。 – Brayn 2009-09-07 17:25:35

相關問題