我目前得到了这个:
var xnumLow = 3000;
var xnumHigh = 4900;
var ynumLow = 9969;
var ynumHigh = 13900;
var ts = Math.round((new Date()).getTime() / 1000);
for (y=ynumLow; y<ynumLow; y++)
{
for(x=xnumLow; x<xnumHigh; x++)
{
$('#box').append(y + " - " + x);
}
}
现在我希望它每10秒附加一个新的整个“行”,所以它们都不会一次性追加。
y“row”是外部for()
循环
我该怎么做?
我得到了:
var refreshId = setInterval(function(){ (...) }, 10000);
但是我不知道在哪里将它与上面的代码合并,以便正常工作。
答案 0 :(得分:1)
setInterval(function () {
// code that appends a box
}, 10000);
https://developer.mozilla.org/en-US/docs/DOM/window.setInterval
答案 1 :(得分:1)
var y = ynumLow;
function addRow()
{
for (x = xnumLow; x < xnumHigh; x++) {
$('#box').append(y + " - " + x);
}
if (y++ < ynumHigh)
refreshId = setTimeout(addRow, 10000);
}
addRow();
编辑为Pete为清晰起见而建议
答案 2 :(得分:1)
(function () {
var xnumLow = 3000,
xnumHigh = 4900,
ynumLow = 9969,
ynumHigh = 13900,
currentY = ynumLow,
delay = 500,
displayData = function () {
var out = [],
x;
for (x=xnumLow; x<xnumHigh; x++) {
out.push( currentY + "-" + x );
}
console.log(out.join(",")); //do the append here
currentY++;
if (currentY<ynumHigh) {
window.setTimeout(displayData,delay);
}
};
displayData()
})();
答案 3 :(得分:0)
我会这样做:
var xnumLow = 3000;
var xnumHigh = 4900;
var ynumLow = 9969;
var ynumHigh = 13900;
var x, y = ynumLow; //don't forget to declare your variables!
var ts = Math.round((new Date()).getTime() / 1000);
(function addYRow() { //Create a function that adds the X elements
for(x=xnumLow; x<xnumHigh; x++)
{
$('#box').append(y + " - " + x);
}
y++; //don't forget to increment y
if(y < ynumHigh) { //only re-call if we aren't done yet
setTimeout(addYRow, 10000); //Recall the function every 10 seconds.
}
}());
看看其他一些答案,重要的是要意识到你不想在给定点10秒内设置一堆事情(如果你做一个循环调用会发生这种情况{{1相反,我假设您要添加一行,然后等待10秒,然后添加另一行。这只能通过添加一行(usible,在我的情况下,setTimeout()
函数)来实现,然后在重新调用add-a-row函数之前延迟10秒。
列延迟:
在回答有关如何在x行中延迟500ms的问题时,这有点棘手,但并不算太糟糕。你只需要再次嵌套东西了:
addYRow()
请注意,如果要延迟列/行添加的开始(例如,如果要在添加行和添加第一列之间设置500毫秒的延迟,则需要调整var y = ynumLow; //don't forget to declare your variables!
var ts = Math.round((new Date()).getTime() / 1000);
(function addYRow() { //Create a function that adds the X elements
var x = xnumLow;
(function addXCol() { //Create a function that adds each X element
$('#box').append(y + " - " + x);
x++;
if(x < xnumHigh) { //if x is not done, call addXCol 500ms later
setTimeout(addXCol, 500);
} else {
y++;
if(y < ynumHigh) { //If x is done but y isn't, call addYRow 10 seconds later
setTimeout(addYRow, 10000); //Recall the function every 10 seconds.
}
}
}());
}());
表达式创建如下所示:
addXCol()
这将使最初的延迟进入。希望有所帮助。
答案 4 :(得分:-1)
这样的东西?
var refreshId = setInterval(function(){
$('#box').append(++y + " - " + x);
}, 10000);