2 回答
TA贡献1805条经验 获得超10个赞
在检查计数器的 if 中添加您最后想要做的事情。
这是一个工作示例:https : //jsfiddle.net/mvhL9ct2/1/
JS:
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++;
// do something
T.document.write('Where are you?');
T.document.write("<br/>");
if (counter === howManyTimes) {
T.document.write('This text needs to be placed last.');
T.document.write("<br/>");
clearInterval(interval);
}
console.log(counter, 'iteration');
},
howOften)
T.document.close(); // problem
}
checkInIntervals(3, 1000);
TA贡献1802条经验 获得超10个赞
文本T.document.write('This text needs to be placed last.')首先出现,因为传递给的函数setInterval不会立即执行。setTimeout被调用后继续执行,该函数之后的下一个命令是T.document.write('This text needs to be placed last.')
为了使这个文本最后你应该把它放在clearInterval函数之前
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++;
// do something
T.document.write('Where are you?');
T.document.write("<br/>");
if (counter === howManyTimes) {
T.document.write('This text needs to be placed last.');
T.document.write("<br/>");
clearInterval(interval);
return;
}
console.log(counter, 'iteration'); },
howOften)
T.document.close(); // problem
}
checkInIntervals(3, 1000);
添加回答
举报