【问题标题】:How to put text at last in a setInterval function?如何将文本最后放入 setInterval 函数?
【发布时间】: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


【解决方案1】:

文本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);

【讨论】:

  • 仍然不是 100% 正确,消息仍然不会是最后一件事
  • 请立即查看
【解决方案2】:

在检查计数器的 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);

【讨论】:

  • 你的代码给控制台错误和永远循环——你需要测试你是否被允许打开一个窗口
  • 来自 jsfiddle。
  • 你的 JSFiddle 也会循环
  • 谢谢,你能把标题改正吗?不是第一次出现,循环开始时出现,我们可以在创建窗口后写T.document.title = 'MIKE!';,但是这样写两次,我们可以写一次并且一直有标题吗?
  • 我建议您使用 appendChild 或任何其他替代方案,少用 document.write。该方法替换了所有现有的html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-28
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多