2012-01-05 70 views
2

我不敢相信我遇到了這個問題。我的setTimeout是:Javascript setTimeout參數問題(我正在使用匿名函數版本)

setTimeout(function(){showContent(entries, 0);}, 500); 

我也試過:

(function(entries, index){ 
    clear_show_content = setTimeout(function(){ 
               showContent(entries, index); 
               }, 500); 
          })(entries, 0); 

showContent(entries, index)指數是不確定的。請告訴我,我在這裏只是錯過了一些明顯的東西。

編輯:此代碼是造成這麼多的問題,今晚:)

var clear_show_content; 
function showContent(json){ 
    var entries = json; 
    $('#content').html(''); 
    if(checkIfNoneEntered(entries)) 
     $('#content').append('<div class="entry">No Alumni Entered Yet!</div>'); 
    else if(checkIfNoMatches(entries)) 
     $('#content').append('<div class="entry">No Matches Found!</div>'); 
    else if(checkIfError(entries)) 
     $('#content').append('<div class="entry">There was an error!</div>'); 
    else { 
     clearTimeout(clear_show_content); 
     $('#content').append('<table border="2" id="content_table" width="50%">'); 
     var filler = '<img width="1" height="1" />'; 

     clear_show_content = setTimeout((function(){showContent(entries, 0);}), 
                      500); 
    } 
} 

function showContent(entries, index){ 
    if(index < 0) 
     return; 

    stop = index + 10 > entries.alumnus.length ? entries.alumnus.length : 10; 
    start = new Date(); 
    for(allIndex = index; allIndex < stop; allIndex++){ 
}//This is where it becomes proprietary, but I highly doubt the issue is after here 

編輯2:這裏的問題進行的jsfiddle。它不適用於我的瀏覽器(Chrome 16),我目前無法訪問任何其他瀏覽器。我認爲這不是問題,因爲我已經編寫了數百次這樣的代碼。 http://jsfiddle.net/eygraber/NduqY/1/

+0

代碼看起來不錯,你肯定_this_是什麼突破? – 2012-01-05 08:32:24

+0

我已經過了一個小時。沒有別的。 – Eliezer 2012-01-05 08:32:54

+3

如果'showContent'的聲明是'showContent(entries,index)',那麼當您通過數字文字時,我不會看到'index'可能未定義。你是否可以展示整個函數,或者你從哪裏運行'setTimeout()'的一些背景? – nnnnnn 2012-01-05 08:33:04

回答

4

你定義你showContent函數兩次...

+0

js中沒有超載? – Eliezer 2012-01-05 08:51:57

+2

你重寫它,這就是問題 - 你的第一個被擦掉,永遠不會被稱爲 – 2012-01-05 08:52:27

+0

不,你不能重載基於參數計數的函數:)我有代碼,實際上如果你_really_想要它(通過計算參數並分配到相應的函數),但對於幾乎所有的東西都是過度的。 – 2012-01-05 08:53:41

4

這兩個函數都被稱爲'showContent',這意味着當您嘗試調用第一個函數時,您確實會調用第二個函數,因爲第一個函數被覆蓋。

當您檢查索引是否未定義時,請檢查條目中的內容。我敢打賭,這是你打算傳遞給第一個函數的json。