There is a timer with reference to the current time. But in some browsers (as an example of FF in Windows 10) it calculates seconds by 2, or seconds just quickly fly by and then goes to minus
$scope.time = {}; $scope.time.s = "00"; $scope.time.m = "2"; $scope.oldTime = new Date().getTime(); $scope.endTime = Math.floor(($scope.oldTime + 120000) /1000); $scope.twoMin = Math.ceil(Math.abs(($scope.endTime - ($scope.oldTime/1000)))); var value = form.phone.substr(0, 2); var config = configs.mobile_codes; $scope.tick = function () { $scope.timeNow = new Date().getTime(); $scope.timeNow = Math.floor($scope.timeNow/1000); $scope.buttonActive = false; if ($scope.timeNow >= $scope.endTime) { $interval.cancel($scope.timer); $scope.avail = true; $scope.time.s = '00'; $scope.time.m = '0'; $scope.buttonActive = true; } else { $scope.twoMin--; $scope.time.s = ($scope.twoMin % 60) < 10 ? "0" + $scope.twoMin % 60 : $scope.twoMin % 60; $scope.time.m = Math.floor($scope.twoMin / 60); } }; $scope.timer = $interval($scope.tick, 1000); Html
<span class="help-block">{{ 'AUTH.CODE-RE' | translate }}: {{time.m}}:{{time.s}}</span> I tried it and it also clicks. That is delayed, it clicks for 2 seconds. as a result, it is totally up to the time that is needed. But jumps there and there.
// Set the date we're counting down to var countDownDate = new Date().getTime() + 120000; // Update the count down every 1 second var x = setInterval(function() { // Get todays date and time var now = new Date().getTime(); // Find the distance between now an the count down date var distance = countDownDate - now; // Time calculations for days, hours, minutes and seconds var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); // Display the result in the element with id="demo" document.getElementById("demo").innerHTML = minutes + "m " + seconds + "s "; // If the count down is finished, write some text if (distance < 0) { clearInterval(x); document.getElementById("demo").innerHTML = "EXPIRED"; } }, 1000);
new Date().getTime() + 120500;- MrFylypenko