TEL:400-8793-956
当前位置:程序、服务器

如何在网页上添加计时器

提问者: 近期获赞: 浏览人数: 发布时间:2020-12-20 15:26:09

 问:我想在网页上包含一个计时器,该计时器旨在像本网站一样进行在线测试,其链接如下http://www.indiabix.com/online-test/aptitude-test/random。

请建议我该怎么做。
 
 
答:另外,在这里我正在编写一些代码来创建一个简单的Timer StopWatch,您可以根据需要对其进行修改。
码:
 
脚本:
window.onload = function()
{
  秒表('开始');
}
var sec = 0;
var min = 0;
var hour = 0;
秒表功能(文本){
   秒++;
  如果(秒== 60){
   秒= 0;
   min = min +1; }
  其他{
   min = min; }
  如果(min == 60){
   min = 0; 
   小时+ = 1; }
 
if(sec <= 9){sec =“ 0” + sec; }
   document.clock.stwa.value =((hour <= 9)?“ 0” + hour:hour)+“:” +((min <= 9)?“ 0” + min:min)+“:” +秒
 
  if(text ==“ Start”){document.clock.theButton.value =“ Stop”; }
  if(text ==“ Stop”){document.clock.theButton.value =“开始”; }
 
  如果(document.clock.theButton.value ==“开始”){
   window.clearTimeout(SD);
   返回true;}
SD = window.setTimeout(“ stopwatch();”,1000);
}
 
函数resetIt(){
  秒= -1;
  min = 0;
  小时= 0;
  如果(document.clock.theButton.value ==“ Stop”){
  document.clock.theButton.value =“开始”; }
  window.clearTimeout(SD);
 }
 
 
var c = 0
变数
函数stopCount()
{
clearTimeout(t)
}
函数timedCount()
{
document.getElementById('txt')。value = c
c = c + 1
如果(c == 61)
{
警报(“超时”)
stopcount()
}
t = setTimeout(“ timedCount()”,1000)
}
 
HTML:
<表格>
 <输入type =“ text” id =“ txt”>
 <input type =“ button” value =“开始秒表” onClick =“ timedCount()”>
 <p>单击上方的开始按钮以启动秒表。
 
 
脚本:
var time = 0,
    经过的='0.0';
window.setInterval(function()
{
    时间+ = 100;
    经过的时间= Math.floor(time / 100)/ 10;
    if(Math.round(elapsed)==过去){过去+ ='.0'; }
    document.title =过去;
},100);
 
答:使用下面的代码作为解决方案。
 
<span id =“ countdown”> 01:30:10 </ span>
<script src =“ // code.jquery.com/jquery-1.11.2.min.js”> </ script>
<script src =“ // code.jquery.com/jquery-migrate-1.2.1.min.js”> </ script>
<script type =“ text / javascript”>
    (功能(){
  $(document).ready(function(){
      var time =“ 01:30:10”,
      零件= time.split(':'),
      小时= + parts [0],
      分钟= +份[1],
      秒= +部分[2],
      跨度= $('#countdown');
 
    函数correctNum(num){
      返回(num <10)?(“ 0” + num):num;
    }
 
    var timer = setInterval(function(){
        秒-;
          if(seconds == -1){
            秒= 59;
              分钟 - ;
 
              if(分钟== -1){
                分钟= 59;
                  小时 - ;
 
                  if(hours ==-1){
                  alert(“计时器完成”);
                  clearInterval(timer);
                  返回;
                }
            }
        }
          span.text(correctNum(hours)+“:” + correctNum(minutes)+“:” + correctNum(seconds));
    },1000);
  }); 
})()
 
</ script>
上一篇: 如何解决内部服务器错误(数据库)werkzeug:请求时出错:
下一篇: 全局和局部变量声明之间的区别