使用moment.js计算经过的时间? [英] Using moment.js to calculate elapsed time?

查看:551
本文介绍了使用moment.js计算经过的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var currentTimeArray = [];
  function currentTime(){
    var time = moment();
    return currentTimeArray.push(time);
  }

  $('.next-fieldgroup').on('click', function(e){
    e.preventDefault();
    currentTime();

    var endTime = $(currentTimeArray).last();
    var startTime = currentTimeArray[0];
    var duration = moment.duration(endTime.diff(startTime));
    var elapsedTime = duration().asMinutes();

    $('#timer-counter').text( elapsedTime );
  });

应该怎么做...

  1. 点击立即存储到ARRAY的时间
  2. 单击arr [0]与arr.last()进行比较,以获取经过的时间
  3. 以分钟为单位更新div

我希望输出像:

耗时:12分钟"

我收到以下错误:

未捕获的TypeError:未定义不是函数

Uncaught TypeError: undefined is not a function

推荐答案

jQuery方法返回包裹在jQuery中的对象,因此您的endTime是jQuery包裹的moment对象.而且jQuery没有.diff方法,因此会出现错误.使用.get(0)[0]获取实际力矩对象

jQuery methods return objects wrapped in jQuery, so your endTime is a jQuery wrapped moment object. And jQuery does not have a .diff method so hence the error. Use .get(0) or [0] to get the actual moment object

var endTime = $(currentTimeArray).last().get(0);

这篇关于使用moment.js计算经过的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆