【问题标题】:JavaScript with Timeout Function带有超时功能的 JavaScript
【发布时间】:2020-09-30 21:38:41
【问题描述】:

我正在尝试在

元素,以#showText 作为 ID。但是,无论如何,#showText 元素永远不会出现。我对下面的代码有解释。谁能帮我显示#showText 元素?

var counter = 0;

// Call the update function 2 seconds after first load.  
timeoutID = window.setTimeout("Update();", 2000);

function Update() {
  counter++;
  var textField = document.getElementById("showText");
  /* The value counter changes once every 2 seconds.  */
  textField.innerHtml = "The counter is now at " + counter;

  // Set another timeout for the next count, function calls itself.  
  timeoutID = window.setTimeout("Update();", 2000);
}

// Set event listeners for the buttons.  
document.getElementById("restart").addEventListener("click", function() {
  counter = 0; // Reset counter to 0.  
  Update(); // Call Update() method to start counting from 0.  
});

// Clears time out for timeID, which means Update() will no longer be invoked, and counter stops increasing.  
document.getElementById("stop").addEventListener("click", function() {
  window.clearTimeout(timeoutID);
});
<h1>Timeout Example</h1>
<p>The counter will update every two seconds. </p>
<p>Press RESTART or STOP to restart or stop the count. </p>
<p id="showText"></p>
<section>
  <button type="button" id="restart">RESTART</button>
  <button type="button" id="stop">STOP</button>
</section>

【问题讨论】:

  • @Olian04 不行,我把setTime方法的第一个参数都改了。
  • 看我的回答,你错过了HTML应该大写。

标签: javascript timeout


【解决方案1】:
  1. setTimeout("Update();", 2000) 更改为setTimeout(Update, 2000)
  2. innerHtml 更改为innerHTML

var counter = 0;

// Call the update function 2 seconds after first load.  
timeoutID = window.setTimeout(Update, 2000);

function Update() {
  counter++;
  var textField = document.getElementById("showText");
  /* The value counter changes once every 2 seconds.  */
  textField.innerHTML = "The counter is now at " + counter;

  // Set another timeout for the next count, function calls itself.  
  timeoutID = window.setTimeout(Update, 2000);
}

// Set event listeners for the buttons.  
document.getElementById("restart").addEventListener("click", function() {
  counter = 0; // Reset counter to 0.  
  Update(); // Call Update() method to start counting from 0.  
});

// Clears time out for timeID, which means Update() will no longer be invoked, and counter stops increasing.  
document.getElementById("stop").addEventListener("click", function() {
  window.clearTimeout(timeoutID);
});
<h1>Timeout Example</h1>
<p>The counter will update every two seconds. </p>
<p>Press RESTART or STOP to restart or stop the count. </p>
<p id="showText"></p>
<section>
  <button type="button" id="restart">RESTART</button>
  <button type="button" id="stop">STOP</button>
</section>

【讨论】:

  • 点击RESTART按钮3或4次,你会发现随着多个Update函数的创建,它会越来越快地开始计数。
  • @MT0 是的......但这不是问题所在。问题是“谁能帮我显示#showText 元素?”
【解决方案2】:

您的 textField.innerHTML 有语法错误;你写了innerHtml。我还删除了孤立函数调用,因为您希望它只在我假设的按钮按下时运行。更正一下,现在它可以工作了 - 停止按钮也是 - :

    var counter = 0;

    function Update() {
      counter++;
      var textField = document.getElementById("showText");
      /* The value counter changes once every 2 seconds.  */
      textField.innerHTML = "The counter is now at " + counter;

      // Set another timeout for the next count, function calls itself.  
      timeoutID = window.setTimeout("Update();", 2000);
    }

    // Set event listeners for the buttons.  
    document.getElementById("restart").addEventListener("click", function() {
      counter = 0; // Reset counter to 0.  
      Update(); // Call Update() method to start counting from 0.  
    });

    // Clears time out for timeID, which means Update() will no longer be invoked, and counter stops increasing.  
    document.getElementById("stop").addEventListener("click", function() {
      window.clearTimeout(timeoutID);
    });
    <h1>Timeout Example</h1>
    <p>The counter will update every two seconds. </p>
    <p>Press RESTART or STOP to restart or stop the count. </p>
    <p id="showText"></p>
    <section>
      <button type="button" id="restart">RESTART</button>
      <button type="button" id="stop">STOP</button>
    </section>

【讨论】:

  • 如果这回答了您的问题,请随时投票并标记为解决方案,以便我们也得到我们的观点;)
  • 如果你多次点击RESTART,它会比每两秒更快地开始计数。
【解决方案3】:

你可以:

  • 使用setInterval 而不是setTimeout
  • innerHTMLinnerText 而不是(无效的)innerHtml
  • 将启动、停止、显示计数器和更新计数器的逻辑分开,以便更清楚地了解正在运行的内容和时间,并确保您没有生成额外的计时器:

var counter = 0;
var isCounting = false;
var timeoutID = null;
var textField = document.getElementById("showText");

function Stop() {
  if ( timeoutID )
  {
    self.clearInterval( timeoutID );
  }
}

function Start()
{
  Stop();
  counter = 0;
  timeoutID = self.setInterval(Update, 2000); 
  Show();
}

function Show()
{
  textField.innerText = counter;
}

function Update() {
  counter++;
  Show();
}

// Set event listeners for the buttons.  
document.getElementById("restart").addEventListener("click",  Start );
document.getElementById("stop").addEventListener("click", Stop );
<h1>Timeout Example</h1>
<p>The counter will update every two seconds. </p>
<p>Press RESTART or STOP to restart or stop the count. </p>
<p id="showText"></p>
<section>
  <button type="button" id="restart">RESTART</button>
  <button type="button" id="stop">STOP</button>
</section>

【讨论】:

    【解决方案4】:

    只需在代码中进行一次更改即可使其按预期工作。 只需替换下面的功能块并运行代码:

    函数更新(){ 计数器++; document.getElementById("showText").innerHTML = "计数器现在到了!" +计数器; timeoutID = window.setTimeout("Update();", 2000); }

    这个解决方案应该适合你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 2017-06-25
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      相关资源
      最近更新 更多