2

I use the following javascript to show a countdown timer for shipping that day

var timerRunning = setInterval(
    function countDown() {

        var target = 14; // This is the cut-off point

        var now = new Date();


        //Put this in a variable for convenience

        var weekday = now.getDay();
        var despatchday = 'TODAY!';


        if (weekday == 0) { //Sunday? Add 24hrs

            target += 24;

            despatchday = 'on Monday';

        } //keep this before the saturday, trust me :>



        if (weekday == 6) { //It's Saturday? Add 48hrs

            target += 48;

            despatchday = 'on Monday';

        }



        if ((weekday == 5) && (now.getHours() > target) && (now.getHours() <= 24)) {

            target += 72;

            despatchday = 'on Monday';

        }


        //If between Monday and Friday, 
        //check if we're past the target hours, 
        //and if we are, abort.
        if ((weekday >= 1) && (weekday <= 5)) {

            if ((now.getHours() > target) && (now.getHours() <= 24)) { //stop the clock

                target += 24;

                despatchday = 'tomorrow';

            } else if (now.getHours() > target) { //stop the clock

                return 0;

                despatchday = 'today';

            }
        }



        var hrs = (target) - now.getHours();
        if (hrs < 0) hrs = 0;
        var mins = 59 - now.getMinutes();
        if (mins < 0) mins = 0;
        var secs = 59 - now.getSeconds();
        if (secs < 0) secs = 0;


        var str = 'Order in the next ' + hrs + 'hrs ' + mins + 'mins ' + secs + 'secs for despatch ' + despatchday;

        document.getElementById('countdownTimer').innerHTML = str;



    }, 1000

);

The problem I have is that if I set the cut off time to anything other than a full hour the timer does not work.

The correct output is Order in the next xx hrs, xx mins xx secs for despatch today

If I set

var target = 14; // This is the cut-off point

as 14:30 it gives "Just checking the time"

I assumed that it needed the mins as a decimal but if I set it as 14.5 it is adding 0.5 hrs to the output; ie 23.5hrs 50mins 30secs

I have set up a fiddle here. http://jsfiddle.net/4eu4o6k0/

Ideally I need it to be able to handle time in the format of hh:mm as that is the format of the time stored in the database. Is there a correct way to process partial hours in this type of script?

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
Steve Price
  • 600
  • 10
  • 28
  • 4
    `var xxx = 14:30` is not valid, it should say `Uncaught SyntaxError: Unexpected token :` in your console. You would need to use a string and parse it. – epascarello Dec 15 '14 at 14:14
  • you can use 14.5 as an alternative for 14 hours 30 minutes – Logan Murphy Dec 15 '14 at 14:18
  • Work with seconds instead and format the result to show it properly like you want. – emerson.marini Dec 15 '14 at 14:20
  • Be aware that the client side time could be _anything_ so may not match your server - ie this should be **very** much informational only. – James Thorpe Dec 15 '14 at 14:22
  • @LoganMurphy As stated in my question, if i set it as 14.5 the time then output becomes, for example, 12.5hrs 55mins 25secs which is obviously wrong. Try it on the fiddle and you will see what i mean – Steve Price Dec 15 '14 at 14:24
  • @JamesThorpe, yes, we are aware that clients in a different time zone would not neccesarily see a vaild time, but goods are only shipped to uk clients so in MOST situations it is accurate enough. – Steve Price Dec 15 '14 at 14:28
  • @StevePrice that was more of a response to epascarello's comment :) – Logan Murphy Dec 15 '14 at 16:06

2 Answers2

1

I'd personally advise against writing own code for handling time intervals because it's known to be error-prone. Use moment.js or date.js for such things

Here's sample for Moment.js

Community
  • 1
  • 1
Andriy F.
  • 2,467
  • 1
  • 25
  • 23
1

you need to hand the decimal place of hrs:

var rem =hrs%1;

mins = mins + (rem*60);
hrs = hrs - rem;

if (mins > 59) {
   mins = mins - 60;
   hrs= hrs +1;
}

Also I think you meant to spell dispatch

Steve
  • 1,371
  • 9
  • 13
  • I tried this and although it prevents the .5 hrs showing in the countdown it doesn't allow for part hours in the cut off time so 14.5 shows the same time as 14 – Steve Price Dec 15 '14 at 15:03
  • Regarding the use of 'despatch' over dispatch, this is a UK based company and, as can be seen here http://grammarist.com/spelling/dispatch-despatch/ Despatch has mostly disappeared from the language—except in the U.K., where it appears in place of dispatch about a third of the time, so in fact both spellings are correct even though 'dispatch' is the more widely used. – Steve Price Dec 15 '14 at 15:08
  • I had a typo mins = mins - 60; instead on mins = 60 - mins. for 14 it shows 6 hrs 49 mins and for 14.5 it shows 7 hrs 19 mins. The part hours get added to the minutes, and then it checks to see if that made minutes more than 60. If that is the case it increments the hours and subtracts 60 from the mins to compensate. Also thank you for explaining the spelling I was unaware of that. – Steve Dec 15 '14 at 16:12