全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

js仿iphone秒表功能 计算平均数

js实现类似iphone的秒表,添加平均数功能

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <title>stop watch</title>
  <!--by 0o晓月メ http://www.cnblogs.com/final-elysion/p/6066358.html -->
  <script type="text/javascript">
    //起始计时时间
    var totalStartTime = null;
    var countStartTime = null;
    
    //暂停时的时间
    var stopCountTime = 0;
    var stopTotalTime = 0;

    //保存的计次时间列表
    var countList = [];

    //循环指针
    var changeTime = null;
    var addnewValue = false;
    var begin = false;
    //label & 缓存已经使用的时间
    var countTime = null;
    var totalTime = null;

    beginChange = function(){
      //设置标志位进行控制,避免多线程造成的变量问题
      begin = true;     
      changeTime = setInterval(changeStopWatch,10);
      
      document.getElementById('begin').disabled = true;
      document.getElementById('stop').disabled = false;
      document.getElementById('commit').disabled = false;
      document.getElementById('reset').disabled = true;
    }

    /**
     * 计时器核心方法
     */
    changeStopWatch = function(){
      if(begin){
        totalStartTime = new Date();
        countStartTime = totalStartTime;
        begin = false;
      }else if(addnewValue){
        //重设新的起始时间 暂停的时间点
        countStartTime = new Date();
        stopCountTime = 0;
        addnewValue = false;
      }

      var now = new Date();
      var tempTotal = (now.getTime() - totalStartTime.getTime())/1000 + stopTotalTime;
      var tempCount = (now.getTime() - countStartTime.getTime())/1000 + stopCountTime;
      tempTotal = Math.floor(tempTotal * 100) / 100;
      tempCount = Math.floor(tempCount * 100) / 100;
      //多线程问题有时候会出现这情况
      if(tempTotal < 0 || tempCount < 0){
        console.log('bug')
        return ;
      }
      setTotalTime(tempTotal);
      setCountTime(tempCount);
    }

    stopChange = function(){
      clearInterval(changeTime);

      stopCountTime = countTime;
      stopTotalTime = totalTime;
      
      document.getElementById('begin').disabled = false;
      document.getElementById('stop').disabled = true;
      document.getElementById('commit').disabled = true;
      document.getElementById('reset').disabled = false;
    }

    addNewValue = function (){
      //缓存添加的时间
      var newValue = countTime;
      countList.push(newValue);

      //设置标志位进行控制,避免多线程造成的变量问题
      addnewValue = true;
      
      //刷新页面
      setNewValue(newValue);
      changeAverage();
    }

    changeAverage = function(){
      var total = 0,
        i = 0;
      for(;i<countList.length; i++){
        total = total +countList[i];
      }
      var result = Math.floor(total/i * 100) / 100;
      document.getElementById('average').innerText = secondToTime(result);
      document.getElementById('average-second').innerText = result;
    }

    resetStopWatch = function(){
      totalStartTime = 0;
      countStartTime = 0; 
      stopCountTime = 0;
      stopTotalTime = 0;
      countList = [];
      changeTime = null;
  
      addnewValue = false;
      begin = false;
  
      setCountTime(0);
      setTotalTime(0);

      document.getElementById('result').innerHTML = "";
      document.getElementById('average').innerText = "00:00:00.00";
      document.getElementById('result-second').innerHTML = "";
      document.getElementById('average-second').innerText = "0";
    }

    function secondToTime(time) {
      var result = "";
      if (null != time && "" != time && time > 0) {
        //hour
        if (time >= 60 * 60) {
          result = parseInt(time / 3600);
          if(result< 10){
            result = "0" + result + ":";
          }else{
            result = result + ":"
          }
        }else{
          result = "00:"
        }

        //min
        if (time >= 60) {
          var tempMin = parseInt((time - parseInt(time / 3600) * 3600 )/ 60) ;
          if(tempMin < 10){
            tempMin = "0" + tempMin + ":";
          }else{
            tempMin = tempMin + ":"
          }
          result = result + tempMin;
        }else{
          result = result + "00:";
        }

        //second
        
        var timeStr = time + "";
        var tempSecond = parseInt(time%60);
        
        if(tempSecond < 10){
          tempSecond = "0" + tempSecond;
        }
        if(timeStr.indexOf(".") >= 0){
          tempSecond = tempSecond + timeStr.substring(timeStr.indexOf("."),timeStr.length);
        }
        result = result + tempSecond;
        
      }else{
        result = "00:00:00.00";
      }
      return result;
    }

    getCountTime = function(){
      return document.getElementById('count-Time');
    }

    setCountTime = function(value){
      countTime = value;
      document.getElementById('count-second-Time').innerText = value;
      document.getElementById('count-Time').innerText = secondToTime(value);
    }

    getTotalTime = function(){
      return document.getElementById('total-Time');
    }

    setTotalTime = function(value){
      totalTime = value;

      document.getElementById('total-Time').innerText = secondToTime(value);
      document.getElementById('total-second-Time').innerText = value;
      
    }

    setNewValue = function(value){
      var newNode = document.createElement("div");
      newNode.innerHTML = secondToTime(value);
      
      var oldNode = document.getElementById('result');
      oldNode.appendChild(newNode);

     
      
      var newNode2 = document.createElement("div");
      newNode2.innerHTML = value;
      
      var oldNode2 = document.getElementById('result-second');
      oldNode2.appendChild(newNode2);
    }

    

  </script>
 </head>

 <body >
  <div style="width: 430px;border-width: 2px;border-style: solid;padding: 10px 10px 10px 10px;">
    <input type="button" id ="begin" value="启动" onclick="beginChange()"/>
    <input type="button" id = "stop" value="停止" disabled="true" onclick="stopChange()"/>
    <input type="button" id = "commit" value="计次" disabled="true" onclick="addNewValue()"/>
    <input type="button" id = "reset" value="重置" disabled="true" onclick="resetStopWatch()"/>
    <br />

    <div style="width:200px;margin-top:10px;" >
      <div style="padding-top:20px;">当前次数计时</div>
      <div id="count-Time" >
        00:00:00.00
      </div>
      <div style="padding-top:20px;">总时间计时</div>
      <div id="total-Time" >
        00:00:00.00
      </div>
      <div style="padding-top:20px;">
        <div>平均值</div>
        <div id ="average" >00:00:00.00</div> 
      </div> 
    </div>
 
    <div style="width: 200px;position: absolute;left: 300px;top: 50px;" >
      <div style="padding-top:20px;">当前次数计时(秒)</div>
      <div id="count-second-Time">
        0
      </div>
      
      <div style="padding-top:20px;">总时间计时(秒)</div>
      <div id="total-second-Time">
        0
      </div>
      <div style="padding-top:20px;">
        <div>平均值(秒)</div>
        <div id ="average-second" >0</div> 
      </div>
    </div>
  </div>

  <div style="width:200px;margin-top:21px;">
    添加的次数列表
    <div id="result" >
      
    </div>
  </div>
  
  
  <div style="width: 200px;position: absolute;left: 300px;top:253px;">
    添加的次数列表(秒)
    <div id="result-second" >
      
    </div>
  </div>
  
 </body>
</html>

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


# js  # iphone  # 秒表  # js实现秒表计时器  # js实现计时器秒表功能  # Javascript实现秒表倒计时功能  # javascript 秒表计时器实现代码  # 原生js实现秒表计时器功能  # javascript实现秒表计时器的制作方法  # javascript设计简单的秒表计时器  # js实现简单秒表走动的时钟特效  # 原生js 秒表实现代码  # 魔方在线秒表javascript版  # js实现简单的秒表  # 多线程  # 计时器  # 大家多多  # changeTime  # countList  # stopTotalTime  # addnewValue  # false  # label  # amp  # stopCountTime  # type  # javascript  # script  # null  # countStartTime  # var  # totalStartTime  # countTime  # commit 


相关文章: 建站之星安装需要哪些步骤及注意事项?  ,柠檬视频怎样兑换vip?  可靠的网站设计制作软件,做网站设计需要什么样的电脑配置?  如何通过IIS搭建网站并配置访问权限?  PHP正则匹配日期和时间(时间戳转换)的实例代码  C++ static_cast和dynamic_cast区别_C++静态转换与动态类型安全转换  Python如何创建带属性的XML节点  简易网站制作视频教程,使用记事本编写一个简单的网页html文件?  建站之星如何实现网站加密操作?  如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?  如何在IIS中新建站点并解决端口绑定冲突?  建站主机核心功能解析:服务器选择与网站搭建流程指南  制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?  如何使用Golang table-driven基准测试_多组数据测量函数效率  Thinkphp 中 distinct 的用法解析  如何配置FTP站点权限与安全设置?  北京企业网站设计制作公司,北京铁路集团官方网站?  一键制作网站软件下载安装,一键自动采集网页文档制作步骤?  建站主机选哪家性价比最高?  ui设计制作网站有哪些,手机UI设计网址吗?  北京网站制作的公司有哪些,北京白云观官方网站?  c# 在高并发下使用反射发射(Reflection.Emit)的性能  建站之星免费版是否永久可用?  如何在Golang中引入测试模块_Golang测试包导入与使用实践  如何用西部建站助手快速创建专业网站?  电商网站制作公司有哪些,1688网是什么意思?  装修招标网站设计制作流程,装修招标流程?  网站制作价目表怎么做,珍爱网婚介费用多少?  c++怎么编写动态链接库dll_c++ __declspec(dllexport)导出与调用【方法】  官网建站费用明细查询_企业建站套餐价格及收费标准指南  如何在腾讯云服务器快速搭建个人网站?  平台云上自助建站如何快速打造专业网站?  如何通过云梦建站系统实现SEO快速优化?  清单制作人网站有哪些,近日“兴风作浪的姑奶奶”引起很多人的关注这是什么事情?  教育培训网站制作流程,请问edu教育网站的域名怎么申请?  网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?  如何在服务器上配置二级域名建站?  在线流程图制作网站手机版,谁能推荐几个好的CG原画资源网站么?  整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?  如何通过虚拟主机快速完成网站搭建?  常州自助建站工具推荐:低成本搭建与模板选择技巧  网站制作模板下载什么软件,ppt模板免费下载网站?  PHP 500报错的快速解决方法  Android自定义listview布局实现上拉加载下拉刷新功能  定制建站是什么?如何实现个性化需求?  建站OpenVZ教程与优化策略:配置指南与性能提升  小说建站VPS选用指南:性能对比、配置优化与建站方案解析  建站之星代理商如何保障技术支持与售后服务?  建站主机数据库如何配置才能提升网站性能?  建站上传速度慢?如何优化加速网站加载效率? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。