3

Basically I need quite (probably) a simple thing check if 5 minutes passed after script load.

Script structure should be like this:

var check;

check = 300; //5 minutes

if ( count to 300 seconds >= check) {

//do magic

} else {


}
me01
  • 41
  • 1
  • 2
  • You probably want `setTimeout`, you just don't know it yet. Anyway, see [How to get the hours difference between two date objects?](http://stackoverflow.com/questions/19225414/how-to-get-the-hours-difference-between-two-date-objects?noredirect=1&lq=1) – Theraot Feb 22 '17 at 16:37

2 Answers2

7

Simply save the time when the load event has been triggered and then check against it.

var loadTime;

window.onload = function() {
    loadTime = (new Date().getTime()) / 1000; // convert milliseconds to seconds. 
};

// your code
var currentTime = (new Date().getTime()) / 1000;
if (currentTime - loadTime >= 300) {
    // then more than 5 minutes elapsed.
}
jorgonor
  • 1,679
  • 10
  • 16
2

The easiest and simplest way could be this:

setTimeout(function(){
    // do stuff
}, 300 * 1000); // time in milliseconds
Mr_KoKa
  • 598
  • 4
  • 13
utnaf
  • 377
  • 1
  • 10
  • 2
    For 5 minutes you need `300000` though. –  Feb 22 '17 at 16:37
  • Even though OP asked for checking not for scheduling a task 5 minutes after load time. Maybe the actual intent was what this answer answers though. – jorgonor Feb 22 '17 at 16:40
  • @jorgonor The OP wants to "do magic" after 5 minutes have passed. How exactly is this answer not answering the question...? –  Feb 22 '17 at 16:42