如何使用 setInterval 和clearInterval 进行无限画廊
我正在尝试使用 JavaScript 获取画廊的图片,但遇到问题。
$(document).ready(function() {
var currentPage = 1;
var totalPics = 2;
var picPerPage = 1;
intervalID = window.setInterval(function() {
console.log(currentPage*picPerPage);
if (totalPics > (currentPage*picPerPage)) {
currentPage++;
fetchNextPage(currentPage);
} else {
clearInterval(intervalID);
}
}, 2000);
});
由于某种原因,它会一直循环。我希望它在 currentPage * picPerPage > 的那一刻停止总图片
。
我在 ubuntu 中使用 firefox 3.6.8
更新
谢谢你们。
我意识到问题是由于 fetchNextPage() 函数造成的。
一旦我将其注释掉,循环问题就解决了。
但是,我需要 fetchNextPage 函数来运行 ajax 函数来获取另一组图像。
以下是该功能
function fetchNextPage(currentPage) {
newUrl = currentUrl + currentPage;
$.ajax({
url: newUrl ,
success: function(data) {
// append page 2 themes
$('#themes-list').append(data);
}
});
}
,那么我应该做什么才能让我的无限画廊可以工作?
谢谢。
I am trying to fetch pictures for a gallery using JavaScript, but I experience problems.
$(document).ready(function() {
var currentPage = 1;
var totalPics = 2;
var picPerPage = 1;
intervalID = window.setInterval(function() {
console.log(currentPage*picPerPage);
if (totalPics > (currentPage*picPerPage)) {
currentPage++;
fetchNextPage(currentPage);
} else {
clearInterval(intervalID);
}
}, 2000);
});
For some reason, it will keeping looping. I want it to stop the moment currentPage * picPerPage > totalPics
.
i am using firefox 3.6.8 in ubuntu
update
Thank you guys.
i realise that the issue is due to the fetchNextPage() function.
oncei commented it out, the looping issue is resolved.
however, i need the fetchNextPage function to run a ajax function to fetch another set of images.
below is the function
function fetchNextPage(currentPage) {
newUrl = currentUrl + currentPage;
$.ajax({
url: newUrl ,
success: function(data) {
// append page 2 themes
$('#themes-list').append(data);
}
});
}
so what should i do so that my infinite gallery can work?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码对我有用。我放入了 console.log() 而不是函数,并且没有更正
intervalID
未正确定义。该函数有可能改变值吗?我得到的输出(OSX 上的 Chrome 5.0.375.127):
更新:因此,根据您添加的代码,我有一些问题和评论:
所以这里缺少的是返回的数据......您可以吗?提供一个示例来说明
data
的值是什么?Your code works for me. I put in console.log() instead of the function and did not correct
intervalID
not properly defined. Is it possible the function alters values?The output I got (Chrome 5.0.375.127 on OSX):
UPDATE: So with your added code I have a few questions and comments:
So whats missing here is the data that comes back... can you provide an example of what value
data
is?尝试在
intervalID = window.setInterval
之前添加var
以正确定义 IntervalID 变量。Try adding
var
beforeintervalID = window.setInterval
to correctly define the intervalID variable.亚历克斯的回答暗示了一个可能的问题。
这段代码在没有其他代码的测试环境中运行得很好。
但是,
intervalID
被定义为全局变量。如果您在其他地方执行的代码也为intervalID
定义了一个值,那么它将严重影响这个值。使用
var IntervalID
来限制该变量的范围。Alex's answer alluded to a possible problem.
This code works just fine in a test environment where there is no other code anywhere.
However,
intervalID
is defined as a global variable. If you have code executing elsewhere that also defines a value forintervalID
, then it's going to be stomping all over this one.Use
var intervalID
to limit the scope of this variable.