Javascript setTimeout 参数问题(我正在使用匿名函数版本)
我不敢相信我对此有疑问。我的 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/
I can't believe that I'm having an issue with this. My setTimeout is:
setTimeout(function(){showContent(entries, 0);}, 500);
I also tried:
(function(entries, index){
clear_show_content = setTimeout(function(){
showContent(entries, index);
}, 500);
})(entries, 0);
and in showContent(entries, index)
index is undefined. Please tell me I'm just missing something obvious here.
EDIT: This code is causing so many problems tonight :)
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
EDIT 2: Here's a jsFiddle of the issue. It's not working on my browser (Chrome 16) and I don't currently have access to any other browser. I don't think that's the issue though, as I've written code like this hundreds of times. http://jsfiddle.net/eygraber/NduqY/1/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在两次定义
showContent
函数...You're defining your
showContent
function twice...您的两个函数都称为“showContent”,这意味着当您尝试调用第一个函数时,您实际上是在调用第二个函数,因为第一个函数被覆盖了。
当您检查索引是否未定义时,请检查条目中的内容。我敢打赌这是你想要传递给第一个函数的 json 。
Both your functions are called 'showContent' which means when you try to call the first one, you're really calling the second one because the first is overwritten.
When you check if index is undefined, check what's in entries. I bet it's the json you meant to pass to the first function.