1

In my Angularjs application I have the limitation that if user changes his system date manually, application starts using that date for all UI operations.

I want to detect if user has changed his system date manually to some older date and compare it with server date stored in my cookies and throw him an error. I tried using :

$window.addEventListener('timeupdate', function() {
   alert('I changed');
});

but this didn't worked. Is there a way in Angular to do this.

t.niese
  • 39,256
  • 9
  • 74
  • 101
Nitish Hardeniya
  • 181
  • 2
  • 15
  • 1
    Check this : http://stackoverflow.com/questions/3367505/detecting-changes-to-system-time-in-javascript – Supradeep Dec 29 '16 at 10:29

2 Answers2

0

You can trigger timeupdate event with using of jQuery.

$('#testvid').on('timeupdate', function(){
    console.log('the time was updated to: ' + this.currentTime);
});

Here #testvid is id of html element.

Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
0

As mentioned in my comment, you can use setInterval to check for date change at certain intervals of time. It can be done in Angular like below:

var app = angular.module('dateChecker', []);

app.controller('MainCtrl', function($scope, $interval) {

  function getTodaysDate(){
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();

    if(dd<10) {
        dd='0'+dd
    } 

    if(mm<10) {
        mm='0'+mm
    } 

    today = mm+'/'+dd+'/'+yyyy;
    return today;
  }

  $scope.oldDate = getTodaysDate();

  $interval(function dateChecker() {
    if($scope.oldDate === getTodaysDate()){
        console.log('No change');
    } else {
        console.log('date changed');
    }
  }, 1000);
});

This checks the time every second and updates accordingly. I'm checking only the date above ignoring the time, you can change the condition as required.

Supradeep
  • 3,246
  • 1
  • 14
  • 28