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

我想最后在一个使用 的函数中放置一个文本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();每次运行该函数时都需要新窗口而没有以前的文本。


我怎样才能做到这一点?


守着星空守着你
浏览 142回答 2
2回答

holdtom

在检查计数器的 if 中添加您最后想要做的事情。这是一个工作示例:https : //jsfiddle.net/mvhL9ct2/1/JS:function checkInIntervals(howManyTimes, howOften) {&nbsp; var T = window.open("", "MsgWindow", "width=400,height=600");&nbsp; var counter = 0;&nbsp; var interval = setInterval(function() {&nbsp; &nbsp; &nbsp; T.document.title = 'MIKE!';&nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; // do something&nbsp;&nbsp; &nbsp; &nbsp; T.document.write('Where are you?');&nbsp; &nbsp; &nbsp; T.document.write("<br/>");&nbsp; &nbsp; &nbsp; if (counter === howManyTimes) {&nbsp; &nbsp; &nbsp; &nbsp; T.document.write('This text needs to be placed last.');&nbsp; &nbsp; &nbsp; &nbsp; T.document.write("<br/>");&nbsp; &nbsp; &nbsp; &nbsp; clearInterval(interval);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; console.log(counter, 'iteration');&nbsp; &nbsp; },&nbsp; &nbsp; howOften)&nbsp; T.document.close(); // problem}checkInIntervals(3, 1000);

守候你守候我

文本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) {&nbsp; &nbsp; var T = window.open("", "MsgWindow","width=400,height=600");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var counter = 0;&nbsp;&nbsp; &nbsp; var interval = setInterval(function() {&nbsp;&nbsp; &nbsp; &nbsp; T.document.title = 'MIKE!'; counter++;&nbsp;&nbsp; &nbsp; &nbsp; // do something&nbsp;&nbsp; &nbsp; &nbsp; T.document.write('Where are you?');&nbsp;&nbsp; &nbsp; &nbsp; T.document.write("<br/>");&nbsp; &nbsp; &nbsp; if (counter === howManyTimes) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; T.document.write('This text needs to be placed last.');&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; T.document.write("<br/>");&nbsp; &nbsp; &nbsp; &nbsp; clearInterval(interval);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; console.log(counter, 'iteration'); },&nbsp;&nbsp; &nbsp; howOften)&nbsp; &nbsp; T.document.close(); // problem}checkInIntervals(3, 1000);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript