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;
       });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助