js倒计时效果demo示例

简介: js倒计时效果demo示例
<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>倒计时</title>
  <style type="text/css">
    *{
      margin: 0;
      padding: 0;
    }
    img{
      border: none;
    }
    .timeplane {
        width: 125px;
        height: 40px;
        line-height: 40px;
        background-color: #a6baab;
        border-radius: 20px;
        float: left;
        font-size: 20px;
        font-weight: bold;
        color: #333;
    }
    #timeplane img {
        width: 28px;
        margin-top: 4px;
        margin-left: 20px;
        margin-right: 10px;
        display: block;
        float: left;
    }
  </style>
</head>
<body>
  <div class="timeplane" id="timeplane"><img src="http://api.touchtao.tech/distich/images/time.png"><span id="livetime">00:00</span></div>
  <br />
  <br />
  <br />
  <div>自定义倒计时时间,倒计时结束后执行,停止倒计时,倒计时重新执行</div>
</body>
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
  var timestart = 20;   //定义倒计时时间;
  var timestep = -1;
  var timeID;
  function timecount() {
    $("#livetime").html(formatSeconds(timestart));
      timestart += timestep;
      if(timestart < 0){
        timestart = 20;//时间;  //赋值0  倒计时结束,就变00:00
      return false;
        //倒计时为0时执行xxxx方法   
      }else{
        var endTime = $("#livetime").html();
        // console.log(endTime);
        if(endTime == '00:01'){
          setTimeout(function(){ // ******************倒计时结束了
          alert("倒计时结束了")
        },1000);
        }
      }
      timeID=setTimeout("timecount()",1000);
  }
  formatSeconds = function(timestart) { 
    var theTime = parseInt(timestart);// 需要转换的时间秒 
    var theTime1 = 0;// 分 
    var theTime2 = 0;// 小时 
    var theTime3 = 0;// 天
    if(theTime >= 60) { 
      theTime1 = parseInt(theTime/60); 
      theTime = parseInt(theTime%60); 
      if(theTime1 > 60) { 
        theTime2 = parseInt(theTime1/60); 
        theTime1 = parseInt(theTime1%60); 
        if(theTime2 > 24){
          //大于24小时
          theTime3 = parseInt(theTime2/24);
          theTime2 = parseInt(theTime2%24);
        }
      } 
    } else {
      var result = '00:';
      if(theTime < 10) {
        result += '0'+theTime;
      } else {
        result += theTime;
      }
      return result;
    }
    var result = '';
    if(theTime > 0){ // 秒
      theTime = parseInt(theTime);
      if(theTime < 10) {
        theTime = '0'+theTime;
      }
      result = ""+theTime;
    } else {
      result += "00";
    }
    if(theTime1 > 0) { // 分钟
      theTime1 = parseInt(theTime1);
      if(theTime1 < 10) {
        theTime1 = '0'+theTime1;
      } 
      result = ""+theTime1+":"+result; 
    } else {
      result += "00";
    }
    if(theTime2 > 0) { // 小时
      theTime2 = parseInt(theTime2);
      if(theTime2 < 10) {
        theTime2 = '0'+theTime2;
      } 
      result = ""+theTime2+":"+result; 
    }
    if(theTime3 > 0) { // 天
      theTime3 = parseInt(theTime3);
      result = ""+theTime3+" "+result; 
    }
    return result; 
  }
  timecount();  //倒计时开始
  setTimeout(function(){        //测试5秒后停止倒计时
    // clearTimeout(timeID);    //停止倒计时(都可以用)
    window.clearTimeout(timeID);  //停止倒计时(都可以用)
    timestart = 20;         //时间重新赋值
    timecount();          //再执行倒计时
  },5000);
</script>
</html>

相关文章
|
5天前
|
JavaScript 前端开发
JavaScript解决生日倒计时的问题
JavaScript解决生日倒计时的问题
|
5天前
|
JavaScript 前端开发
JavaScript编写一个倒计时
JavaScript编写一个倒计时
10 0
|
19天前
|
JavaScript 前端开发
JS实现网页页面的框架(demo)
JS实现网页页面的框架(demo)
14 1
|
19天前
|
编解码 JavaScript 前端开发
【专栏】介绍了字符串Base64编解码的基本原理和在Java、Python、C++、JavaScript及Go等编程语言中的实现示例
【4月更文挑战第29天】本文介绍了字符串Base64编解码的基本原理和在Java、Python、C++、JavaScript及Go等编程语言中的实现示例。Base64编码将24位二进制数据转换为32位可打印字符,用“=”作填充。文中展示了各语言的编码解码代码,帮助开发者理解并应用于实际项目。
|
19天前
|
JSON 前端开发 JavaScript
从假数据到动态表格:一个简单的JavaScript和HTML示例
从假数据到动态表格:一个简单的JavaScript和HTML示例
|
19天前
|
JavaScript 前端开发 开发者
JavaScript中的变量提升:解析、应用及示例
JavaScript中的变量提升:解析、应用及示例
20 1
|
19天前
|
JavaScript
js开发:请解释什么是ES6的扩展运算符(spread operator),并给出一个示例。
ES6的扩展运算符(...)用于可迭代对象展开,如数组和对象。在数组中,它能将一个数组的元素合并到另一个数组。例如:`[1, 2, 3, 4, 5]`。在对象中,它用于复制并合并属性,如`{a: 1, b: 2, c: 3}`。
14 3
|
19天前
|
JavaScript
js开发:请解释什么是ES6的默认参数(default parameters),并给出一个示例。
ES6允许在函数参数中设置默认值,如`function greet(name = &#39;World&#39;) {...}`。当调用函数不传入`name`参数时,它将默认为&#39;World&#39;,提升代码简洁性和可读性。例如:`greet()`输出&quot;Hello, World!&quot;,`greet(&#39;Alice&#39;)`输出&quot;Hello, Alice!&quot;。
17 4
|
19天前
|
JavaScript 前端开发
js开发:请解释什么是ES6的解构赋值(destructuring assignment),并给出一个示例。
ES6的解构赋值简化了JavaScript中从数组和对象提取数据的过程。例如,`[a, b, c] = [1, 2, 3]`将数组元素赋值给变量,`{name, age} = {name: &#39;张三&#39;, age: 18}`则将对象属性赋值给对应变量,提高了代码的可读性和效率。