Jump to content


Member Since 01 Jan 1970
Offline Last Active Mar 23 2017 07:22 PM

Posts I've Made

In Topic: JS Open Hours

08 February 2017 - 03:51 AM

So a couple of things. 


Firstly, sure, there are more "elegant" ways of writing this if "make the shortest amount of code possible" is what you consider "elegant". However, I would argue what is more readable and flexible for yourself and other non/semi-coders in the future would be to have a lookup table of hours, Something along the lines of this (I think I got the hours of operation right, based on your condition):

function checkTime() {
    // array of 7 elements, one for each day of the week
    var hoursOfOperation = [
        // array of 24 elements, one for each hour of the day. 
        // 0 value means closed, 1 value means open
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], // sunday    - closed all day!
        [0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0], // monday    - open 9a to 5p
        [0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0], // tuesday   - open 9a to 5p
        [0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0], // wednesday - open 9a to 1p
        [0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0], // thursday  - open 9a to 5p
        [0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0], // friday    - open 9a to 5p
        [0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0]  // saturday  - open 10a to 1p
    var now = new Date();
    var messageDiv = document.getElementById('messageDiv');
    var dayOfWeek = now.getDay();
    var hour = now.getHours();    
    var status = hoursOfOperation[dayOfWeek][hour] ? 'open' : 'closed';
    messageDiv.innerHTML='We are '+status+'!';
} // end checkTime
See, now you can easily update a table for any hour of any day without messing with conditions. 
Secondly, I should point out to you that since javascript is client-side, the date/time is generated by the user's system clock locally set on their computer. Meaning, what their browser says the date/time is may not accurately reflect your actual hours of operation.  Consider someone in Colorado visiting your page, vs. someone in China.. completely different timezone. So, if you want to (more) accurately reflect your actual hours of operation, you will need to include some conversion to your timezone.  But even then, this is no guarantee it will be 100%, because visitors can (and often do, particularly kiddies trying to get around time/date based software restrictions ) change their time settings easy enough. Or alter your javascript.  So if you really truly want the most accuracy out of all this, you should move this stuff server-side.  But there is a snag to that, which is the constant check for current open/close status.
Which brings me to my last point: I don't know what your actual site looks like, but is it really necessary to use setInterval to constantly check and update this? Things like setInterval come with a performance penalty, and I have a sneaking suspicion just showing an updated status on every page load will likely be just fine.. 

In Topic: Contact form - success message in the same page

18 December 2016 - 01:09 AM

yeah well I have a feeling that's not what he really wants. 

In Topic: Contact form - success message in the same page

16 December 2016 - 04:15 AM

You need to restructure your code to use AJAX. Read up on how to submit an AJAX POST request and receive a response to update a div or w/e on the same page.  There are a billion tutorials out there for it.  Come back with specific questions about it if you get stuck. 

In Topic: On close browser.

15 December 2016 - 08:58 PM

Literally the only thing I have ever seen popup when I try to close my tab/browser or otherwise navigate from the page that I only _marginally_ consider "okay" (read: barely tolerable) is a warning about losing unsaved changes to something.  

In Topic: Need help finding right pattern

13 December 2016 - 07:55 PM

At face value, this should work for you:

if ($match) {
  // matched, do something

but i have a sneaking suspicion you aren't telling us the whole story/requirements.