【发布时间】:2020-02-04 21:05:38
【问题描述】:
我想最后在一个使用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();,因为我每次运行函数时都需要新窗口,而没有以前的文本。
我该怎么做?
【问题讨论】:
-
因为间隔是异步的。代码必须在最后一个区间内。
标签: javascript overwrite