1

I want to use javascript or jquery to detect when the day has changes (0:00) and submit an ajax call to the server.

At first I thought about creating an interval object to check, but it seems it will be inneficient since it will be checking every second for a date change...

Perhaps I could calculate between the difference from time.now and midnight, then schedule something that will load at that time. How can I schedule something to work that way in javascript/jquery? Or maybe is there a more reliable solution?

sigmaxf
  • 7,998
  • 15
  • 65
  • 125
  • Is efficiency that much of an issue? setInterval uses up a lot less resources than most people think. I use it as a game timer and it runs at 60 fps with no problem. – Sheerforce Sep 03 '15 at 19:07
  • This might help - http://stackoverflow.com/a/8584315/297641 – Selvakumar Arumugam Sep 03 '15 at 19:08
  • This might help - http://stackoverflow.com/a/8584315/297641 – Selvakumar Arumugam Sep 03 '15 at 19:08
  • I don't really know @Sheerforce . I'm already using an interval for a clock that is displayed in the page, I don't know how much a comparison at every loop (if midnight) would add up to other stuff being processed on the page. Trying to make this as optimized as possible – sigmaxf Sep 03 '15 at 19:19

1 Answers1

1

use setTimeout.

var timeDifference = //time remaining in ms until midnight. 
setTimeout(function(){
  //code you want to run at midnight. 
},timeDifference)
Asad Palekar
  • 301
  • 2
  • 12
  • refer this [SO](http://stackoverflow.com/questions/10944396/how-to-calculate-ms-since-midnight-in-javascript) for calculating time difference. – Asad Palekar Sep 03 '15 at 19:09