0

I am using some JavaScript to inspect a countdown hour minute target and then kick-off a php page:

window.setInterval(function() { // Set interval for checking
    var date = new Date();
    if (date.getHours() === 13 && date.getMinutes() === 31) { // Check the time
        location.href = "real_live.php?lotnum=1";
    }
}, 1000); // Repeat every 1000 milliseconds (1 second)

It's currently using the client side clock and functions perfectly. However, I need to use the server side clock and try as I may I have not found a working way to do this. Can you advise?

3
  • why you need to use server side hour? Commented May 25, 2015 at 12:05
  • What do you mean by using the server side clock - do you want the server to push data to the client rather than the client polling the server? If yes, server side events are one approach. Commented May 25, 2015 at 12:07
  • Build a unix timestamp of desired time and use it to seed the new date. Commented May 25, 2015 at 12:13

3 Answers 3

1

You should have two separate backend scripts for this. One for the timer which the client will trigger, and should it hit the time you want, you trigger your time-sensitive script.

So,

xhr.open("GET", "/time.php", false);
xhr.onload = function (e){
  //do your magic with the results to real_live.php
}
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe he might retrieve the number of seconds remaining instead of comparing dates, but it's a good approach.
0

Use Date.UTC. This ensures that its the same, no matter where the client machine is located. You can read more on the syntax here

2 Comments

what if client change the system date ?
The UTC would give you UTC equivalent of the time from system time. If you fear that the client has access to change their system time, then you should initiate the variable on page load from Server time (which is reliable) and count seconds from that point on
0

With a little more research I have found a solution, which I believe is accurate. The question was asked in order to find the server-side time, which not all the answers did. But I believe that this Ajax abridged code does the trick.

	window.setInterval(function(){ // Set interval for checking
		var xmlHttp;
		function srvTime(){
		try {
			//FF, Opera, Safari, Chrome
			xmlHttp = new XMLHttpRequest();
		}
		catch (err1) {
			//IE
			try {
				xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
			}
			catch (err2) {
				try {
					xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
				}
				catch (eerr3) {
					//AJAX not supported, use CPU time.
					alert("AJAX not supported");
				}
			}
		}
		xmlHttp.open('HEAD',window.location.href.toString(),false);
		xmlHttp.setRequestHeader("Content-Type", "text/html");
		xmlHttp.send('');
		return xmlHttp.getResponseHeader("Date");
		}

		var st = srvTime();
		var date = new Date(st);
		if(date.getHours() === 14 && date.getMinutes() === 40){ // Check the time
			location.href = "real_live.php?lotnum=1";
		}
	},1000); // Repeat every 1000 milliseconds (1 second)

But thank you all very much for your input, which led me to this solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.