The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

if =

Hello, I have been working on this for some time and am stumped and really need help. This shouldn't be hard for those that understand javascript better than I do.

For some reason the error message I get for this says it wants a paranthesis here right after 24: "if (tm() = 24)"
Which makes no sense because current time results from javscript should look like this 00:00:00. The first part being the hours, second being the minutes, and last being the seconds. So I don't know why it would want a paranthesis there. But when I do put a paranthesis there just to remove the error, it doesn't refresh the page at the right time anyway.

Here's the code: All it supposed to do is get the current time for the day. If the time for the day is midnight, it is supposed to do a setTimeout in 3 seconds after midnight.

1) Why are you using $ sign for variable names? This is Javasript!
2) Missing ending semicolon
3) Function returned nothing, you were expecting a string
4) 24:60:60 is invalid in 2 ways, A) Its a string, so must be quoted, B) There is no 24 hour digit, 24 is actually 0 hours
5) Theres more flexibility if you compared each hr, mn and sc digit seperately than a rigid combined string!
6) You used = assignment operator, should have been == comparison operator
7) Could have put everything inside a function, no need to put the if conditional outside
8) Flawed logic #1, to reload a page only within a one second window! My code below reloads within a 3 second window is just as ridiculous. The page will never reload as you intended.
9) Flawed logic #2, javascript is client side, my midnight may be the servers 10PM.

I read where the the 24th hour is actually the 23rd? So if it gets past midnight it is supposed to do a page refresh in about 4 or 5 seconds. Can anyone spot why the page refresh isn't working? Thank you.

1Jen, I thought you wanted to do it on a page load, if you need the script to run while a user is on the page, then use setInterval, you can also use setTimeout, but it is easier in your case to use setInterval. Here is an example.

I really appreciate getting help with this, I thought getting a page refresh based on what time it is would be easy, but its a lot more work than I thought. Thanks for the great link on setTimeout etc.

It isn't working so I thought I better set up an example scenario using this code, so that hopefully someone can tell me what I am still doing wrong. I'll put my comments below, so you can see what my thinking is when testing this code.

Code:

function tm()
{
var d = new Date();
var hr = d.getHours();
var mn = d.getMinutes();
var sc = d.getSeconds();
if ( hr == 20 && mn == 1 && sc < 5 ) // I want to test this code to see if the page will refresh for just after 8:00 pm so why the time is listed with these numbers here.
{
// self.location.reload(true)
document.location.href="http://www.website.com/test.html#hello"; // i am using this to test, so I can see #hello if it works I put the full path to the file.
//clearInterval(midnight_runner); // No need for this on your live script, since it runs only once
}
}
var chk_d = new Date();
var chk_hr = chk_d.getHours();
if ( chk_hr > 19 && chk_hr < 23 ) // I want to test this code between 7-12 pm pm so why these numbers are listed here. (user time)
{
var midnight_runner = setInterval( tm, 10000 ); // run tm() keeping 10 seconds here for testing just to see if the page refresh occurs at the time listed in the first time variables listed at hr == 20...
}

Am I setting this code up correctly? Because when I run it this way, no refresh happens and the word hello doesn't appear in the browser bar.

1Jen, you have to be very literal when scripting in general, like, run script only on 19 hours, in the first minute and not all of the first minute, only the first x seconds, reason why we need to the seconds detail? If the interval time is 1 second lets say, the page will reload once say on 19:01 as expected, then as the page reloads, if its still 19:01, the script will run again and the page will reload again, so we specify down to seconds when the page should reload.

In my example script I made a big mistake, since the interval was 30 seconds, the sc comparison has to be at least that value. (I have corrected this on my original post.) But I admit I'm a little confused as to which is the best seconds condition there, I'm thinking it should be fine if it matches the interval time.

Edit: Glad you got it working! I posted this before reading your reply sigh. anyways here my new code ...

Have adjusted the script, it should work as is this time, just copy paste the whole thing into the <head> part of your page, remove the comments if you want later on. Read it as it is, don't reinterpret it.

Code JavaScript:

// This is a stopper, it will check if string "midnight_passing" in the URL as a query string// If exist, don't run this code anymore, our job is done. Otherwise, we have work to do.if( window.top.location.search.substring(1).indexOf('midnight_passing')==-1){// PART 1: Define the "precise" time checker and reloaderfunction midnight_reloader(){var d =new Date();// Some hardcoded values to simulate time, only for testingvar hr =0;//d.getHours();var mn =1;//d.getMinutes();var sc =10;//d.getSeconds();// change this to fit your needs or just leave it// This will run on 0 hours, on the first minute between 0 and 10 seconds if( hr ==0&& mn ==1&& sc <11){// If its now 00:01:10, this script will run// On your URL append "?midnight_passing" to the back of it like here// we need this to check if the page was reloaded by this script
document.location.href="test.html?midnight_passing";}}// PART 2 : Setup a timer and a loose time checker// Setup setInterval() callback timer, because we do not need this running // anytime except near midnight, otherwise its kind of wastefulvar chk_d =new Date();// simulating time, for testingvar chk_hr =0;//chk_d.getHours();var chk_mn =1;//chk_d.getMinutes();// If user is here between 11:45PM and 12:02AM, then we need to prepare to reload the page// this is not precise, we depend on midnight_reloader() instead to check the time further// Remember that this part runs only on page load, once only.if((chk_hr ==23&& chk_mn >45)||(chk_hr ==0&& chk_mn <2)){var midnight_timer = setInterval( midnight_reloader,10000);// But setInterval, will run midnight_reloader() every 10 seconds if all conditions have been met// So our midnight_reloader() will do the work of checking if its midnight every 10 seconds}}

Know that without the setInterval() callback, this script will run only once when the page loads, setInterval() will call the function in its first argument again and again, the 2nd argument is the gap between each call. Remember otherwise, the script will run only once, so there will be little chance of the page auto reloading as you wished.

Dude, no need to send me anything!! Your thanks is enough, and besides, I gain much from sitepoint forums, even for your example I am learning stuff there, just spreading some love back, remember the currency in use here is knowledge ( hehee or something like that, what do I know.) And you don't have to pay it back to me directly.