Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
149
rated 0 times [  154] [ 5]  / answers: 1 / hits: 5407  / 10 Years ago, tue, august 19, 2014, 12:00:00
  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 );
});


What it is supposed to do...




  1. on click store NOW time in to ARRAY

  2. on click compare arr[0] with arr.last() to get time elapsed time

  3. update div with elapsed time in minutes



I expect the output to like:



Time elapsed: 12 minutes



I get the following error:




Uncaught TypeError: undefined is not a function



More From » jquery

 Answers
1

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);

[#43025] Monday, August 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylanalis

Total Points: 438
Total Questions: 85
Total Answers: 102

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
kylanalis questions
Sat, Oct 2, 21, 00:00, 3 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
Thu, Feb 13, 20, 00:00, 4 Years ago
Tue, Jan 7, 20, 00:00, 4 Years ago
;