2012-01-05 66 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。