dtvp3625 2016-01-15 08:44
浏览 148
已采纳

获取我的页面开始运行angularjs的时间

I am making an app displaying the current system time but I want to get the time my page started running so I can calculate and display the time that my page is up and running. I am using angularjs and currently has no idea on how can I get this. I have my code on getting the current system time like this

 Current time is:
      <span my-current-time="format1"></span>
      <span my-current-time="format"></span> :
      <span my-current-time="format3"></span> :
      <span my-current-time="format4"></span> 
      <span my-current-time="format5"></span>

with this script

 $scope.format1 = 'M/d/yy  ';
      $scope.format = 'h';
      $scope.format3 = 'mm';
      $scope.format4 = 'ss';
      $scope.format5 = 'a';
      $scope.format2 = 'Z ';

and a directive like this

.directive("myCurrentTime", function(dateFilter) {
            return function(scope, element, attrs) {
              var format;
                scope.$watch(attrs.myCurrentTime, function(value) {
                  format = value;
                  updateTime();

                });
              function updateTime() {
                var dt = dateFilter(new Date(), format );
                         element.text(dt);
                      }

                      function updateLater() {
                        setTimeout(function() {
                          updateTime(); // update DOM
                          updateLater(); // schedule another update
                        }, 1000);
                      }

                      updateLater();
                    }
                  });

I just want to display the total hours my page is currently running

  • 写回答

1条回答 默认 最新

  • douzepao0281 2016-01-15 08:55
    关注

    first save the page load value... $scope.pageLoad = new Date()

    and then use a filter to display that value

        <p>running since : {{pageLoan | timespan}}</p>
    

    and define timespan filter

      angular.filter(timespan, function(time){
             var now = new Date();
             return (now - time) / 1000;
       });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集