我想在使用setInterval
的函数中最后放置一个文本,但是我不能这样做,代码在下面给出-
function checkInIntervals(howManyTimes, howOften)
{
var T = window.open("", "MsgWindow","width=400,height=600");
var counter = 0;
var interval = setInterval(function()
{
T.document.title = 'MIKE!'; counter++;
if (counter === howManyTimes)
{
clearInterval(interval);
}
// do something
T.document.write('Where are you?');
T.document.write("<br/>");
console.log(counter, 'iteration');
}, howOften)
T.document.write('This text needs to be placed last.');//problem
T.document.write("<br/>");
T.document.close(); // problem
}
checkInIntervals(3, 1000);
[这里,T.document.write('This text needs to be placed last.');
首先出现,然后由于T.document.close();
而消失,但是我需要“此文本必须放在最后。”为了最后出现,我每次运行该函数时都需要T.document.close();
,因为我需要一个新窗口,而没有以前的文本。
我该怎么做?
如果要检查计数器,则在最后添加您想做的事情。
文本T.document.write('This text needs to be placed last.')
首先出现,因为传递给setInterval
的函数不会立即执行。调用setTimeout
之后,继续执行,该函数之后的下一个命令为T.document.write('This text needs to be placed last.')