2013-02-04 26 views

回答

2

給它一個標識符:

(function named() { 
    if (some scripts loaded) { 
     otherFunction(); 
    } else { 
     window.setTimeout(named , 100); 
    } 
})(); 

這是什麼被稱爲「命名的函數表達式」。該標識符僅在範圍內其所指的功能。

請勿使用arguments.callee,因爲它已被棄用,並且會在嚴格模式下實際引發語法錯誤。

+0

這只是在關閉中「可見」。 –

0

如果你想避免產生任何新的頂級功能的名稱,你可以把你的代碼在本地的功能,讓你一個函數名都會執行最初並傳遞給setTimeout()

(function() { 
    function doit() { 
     if (some scripts loaded) { 
      otherFunction(); 
     } else { 
      window.setTimeout(doit, 100); 
     } 
    } 
    // execute the first time 
    doit(); 
})(); 
+0

接受@James Allardice的答案,因爲它更簡單,並尊重範圍。 – DhruvPathak