js实现倒计时器自定义时间和暂停


Posted in Javascript onFebruary 25, 2019

js倒计时器可自定义时间和暂停,效果如下,点击start 开始计时,end结束计时

js实现倒计时器自定义时间和暂停

分别复制 H5 css js 代码即可实现,具体的算法在js控制函数中(都写了注释)

css

html,body{
width:100%;height:100%;
}
.content{
height:100%;width:100%; 
}
.row-center{
display:flex;flex-direction:row;justify-content:center;
align-items:center;
}
.tc-input-style{
outline:none;border:none;width:20%;height:80%;border-radius:10px;
}
.tc-span-style{
width:30%;height:100%;font-weight:bold;
}
.tc-font-style{
font-size:15px;font-weight:bold;
}
.tc-div-style1{
height:33%;width:100%
}
.tc-div-style0{
height:30%;width:100%
}
.tc-div-style2{
height:10%;width:100%; 
}
.tc-div-style3{
height:100%;width:30%
}
.column-center{
display:flex;flex-direction:column;justify-content:center;
align-items:center;
}
.column-start-center{
display:flex;flex-direction:column;justify-content:flex-start;
align-items:center;
}
#timecount{
height:50%;width:20%;
}
.button-style-0{
border-radius:10px;
}
.row-space-around{
display:flex;flex-direction:row;justify-content:space-around;
align-items:center;
}

h5

<body>
<div class="content row-center"> 

  <div id="timecount" class="column-center tc-font-style" >
  <div class="column-start-center tc-div-style0" >
  <div class="row-center tc-div-style1" ><span class="tc-span-style row-center">小时:</span><input class=tc-input-style id="hour_count" value="0"> </div>
  <div class="row-center tc-div-style1" ><span class="tc-span-style row-center">分钟:</span><input class=tc-input-style id="minute_count" value="0"> </div>
  <div class="row-center tc-div-style1" ><span class="tc-span-style row-center">秒:</span><input class=tc-input-style id="second_count" value="0"> </div>
  </div>

  <div class="row-center tc-div-style2">
  <div class="row-center tc-div-style3" id="hour_show" ></div>
  <div class="row-center tc-div-style3" id="minute_show"></div>
  <div class="row-center tc-div-style3" id="second_show"></div>
  </div>

  <div class="row-space-around tc-div-style2">
  <button class="button-style-0" onclick="timecounts()">start</button>
  <button class="button-style-0" onclick="timestop()">stop</button>
  </div>

  </div>


</div>
</body>

记得引入jq

<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>

JS

<script type="text/javascript">
var timecount;//定义一个全局变量
function timer(intDiff){
 //定义一个循环函数每一秒定时执行
 timecount=setInterval(function(){
 var hour=0,
 minute=0,
 second=0;//初始化时间默认值 
 //算法控制
 if(intDiff > 0){
 hour = Math.floor(intDiff / (60 * 60)) ;
 minute = Math.floor(intDiff / 60) - (hour * 60);
 second = Math.floor(intDiff) - (hour * 60 * 60) - (minute * 60);
 }
 if (minute <= 9) minute = '0' + minute;
 if (second <= 9) second = '0' + second;
 //打印到dom
 $('#hour_show').html('<s id="h"></s>'+hour+'时');
 $('#minute_show').html('<s></s>'+minute+'分');
 $('#second_show').html('<s></s>'+second+'秒');
 intDiff--;
 }, 1000);
 console.log(intDiff); 
}

function timecounts(){
 console.log($("#hour_count").val())
 count=parseInt($("#hour_count").val()*3600)+parseInt($("#minute_count").val()*60)+parseInt($("#second_count").val())
 timer(count);//调用计时器函数
 console.log(count);
}
function timestop(){
 var hour= $("#hour_show").text();
 var minute= $("#minute_show").text();
 var second= $("#second_show").text();
 var time=parseInt($("#hour_show").text())*3600+parseInt($("#minute_show").text())*60+parseInt($("#second_show").text())
 console.log(time);
 timecount=window.clearInterval(timecount);//停止计时器
}
</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript应用:Iframe自适应其加载的内容高度
Apr 10 Javascript
关于jQuery UI 使用心得及技巧
Oct 10 Javascript
js实现瀑布流的一种简单方法实例分享
Nov 04 Javascript
jquery操作HTML5 的data-*的用法实例分享
Aug 17 Javascript
jQuery Ajax()方法使用指南
Nov 19 Javascript
jquery div模态窗口的简单实例
May 28 Javascript
浅谈Angular4实现热加载开发旅程
Sep 08 Javascript
jQuery实现列表的增加和删除功能
Jun 14 jQuery
vue.js删除列表中的一行
Jun 30 Javascript
使用Sonarqube扫描Javascript代码的示例
Dec 26 Javascript
从0到1学习JavaScript编写贪吃蛇游戏
Jul 28 Javascript
vue+ElementUI 关闭对话框清空验证,清除form表单的操作
Aug 06 Javascript
JS module的导出和导入的实现代码
Feb 25 #Javascript
js实现多个倒计时并行 js拼团倒计时
Feb 25 #Javascript
js实现网页同时进行多个倒计时功能
Feb 25 #Javascript
js实现一个页面多个倒计时的3种方法
Feb 25 #Javascript
Vue自定义指令上报Google Analytics事件统计的方法
Feb 25 #Javascript
写一个Vue Popup组件
Feb 25 #Javascript
利用Webpack实现小程序多项目管理的方法
Feb 25 #Javascript
You might like
关于IIS php调用com组件的权限问题
2012/01/11 PHP
深入PHP5中的魔术方法详解
2013/06/17 PHP
Aster vs Newbee BO5 第三场2.19
2021/03/10 DOTA
关于JS管理作用域的问题
2013/04/10 Javascript
jquery日历控件实现方法分享
2014/03/07 Javascript
深入解析JavaScript的闭包机制
2015/10/20 Javascript
详解AngularJS控制器的使用
2016/03/09 Javascript
js实现按钮控制带有停顿效果的图片滚动
2016/08/30 Javascript
原生JS实现图片轮播切换效果
2016/12/15 Javascript
Angular下H5上传图片的方法(可多张上传)
2017/01/09 Javascript
PHP7新特性简述
2017/06/11 Javascript
Node.js学习之TCP/IP数据通讯(实例讲解)
2017/10/11 Javascript
vue webpack开发访问后台接口全局配置的方法
2018/09/18 Javascript
ajax跨域访问遇到的问题及解决方案
2019/05/23 Javascript
layer父页获取弹出层输入框里面的值方法
2019/09/02 Javascript
layui 数据表格 点击分页按钮 监听事件的实例
2019/09/02 Javascript
微信浏览器左上角返回按钮监听的实现
2020/03/04 Javascript
使用Angular9和TypeScript开发RPG游戏的方法
2020/03/25 Javascript
JavaScript 如何在浏览器中使用摄像头
2020/12/02 Javascript
Python分析学校四六级过关情况
2017/11/22 Python
名片管理系统python版
2018/01/11 Python
Tensorflow实现卷积神经网络的详细代码
2018/05/24 Python
python实现维吉尼亚加密法
2019/03/20 Python
python机器学习库scikit-learn:SVR的基本应用
2019/06/26 Python
Python中变量的输入输出实例代码详解
2019/07/28 Python
python3中pip3安装出错,找不到SSL的解决方式
2019/12/12 Python
Canvas与Image互相转换示例代码
2013/08/09 HTML / CSS
迷你分体式空调:SoGoodToBuy
2018/08/07 全球购物
PREMIUM-MALL法国:行李、箱包及配件在线
2019/05/30 全球购物
工厂厂长的职责
2013/12/12 职场文书
个人三严三实对照检查材料
2014/09/25 职场文书
计划生育工作汇报
2014/10/28 职场文书
考试作弊检讨书
2015/01/27 职场文书
花田少年史观后感
2015/06/16 职场文书
创业计划书之甜品店
2019/09/18 职场文书
java设计模式--七大原则详解
2021/07/21 Java/Android