简体   繁体   English

如何在 asp.net 内核中使用 JavaScript 创建倒数计时器?

[英]how to create countdown timer using JavaScript in asp.net core?

I'm tying to create a countdown timer for online test using JavaScript in asp.net core.我正在尝试使用 asp.net 内核中的 JavaScript 创建一个用于在线测试的倒数计时器。 I want it to change every 1 second without refreshing the page.我希望它每 1 秒更改一次而不刷新页面。 when I run the program it doesn't show me the countdown.当我运行程序时,它没有显示倒计时。

This my javascript code:这是我的 javascript 代码:

<script type="text/javascript">
    countdown = function () {
    var current = parseInt(document.getElementById("timerLabel").innerHTML);
    document.getElementById("left").innerHTML = current;
    if (current > 0) {
        setTimeout('countdown()', 1000); // run every second
    }}

This is my view: I used foreach loop to show this.这是我的观点:我使用 foreach 循环来展示这一点。

 <div class="panel-footer">
            <p>   @item.Grade</p>
            <p>  @item.StartTime</p>
            <p id="EndTime">  @item.EndTime</p>
            <span id="timerLabel">@ViewBag.LeftTime</span>
            <p id="left"></p>
        </div>
  1. Do not get the timerLabel value inside the function unless you also set it不要获取 function 中的 timerLabel 值,除非你也设置了它

  2. Use interval and clear it when done使用间隔并在完成时清除它

 let current = parseInt(document.getElementById("timerLabel").innerHTML); const countdown = function() { document.getElementById("left").innerHTML = --current || "Done"; // using current when 0 it is false if (current <= 0) clearTimeout(tId); // stop the interval }; const tId = setInterval(countdown, 1000)
 <span id="timerLabel">10</span> <p id="left"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM