如何在setInterval函数中最后放置文本?

问题描述 投票:-1回答:2

我想在使用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
2个回答
0
投票

如果要检查计数器,则在最后添加您想做的事情。


0
投票

文本T.document.write('This text needs to be placed last.')首先出现,因为传递给setInterval的函数不会立即执行。调用setTimeout之后,继续执行,该函数之后的下一个命令为T.document.write('This text needs to be placed last.')

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.