0

having a slightly weird issue that I cant figure out. Ive set up a javascript timer, all it does is repeats an interval every second that checks the difference between 2 dates and displays the results. All seems fine, however when leaving the browser open for several minutes (not touching it.. literally walking away for a while), it seems to "time out" and stop functioning. No console error messages or anything, the code just stops executing.. Was wondering if anyone had any idea what could be causing this? Is my code the issue or is this a built in browser function to stop js functions if there is no input from the user on a page for a certain time?

edit sorry should mention this timer is set to run for around 40 days at the moment so it will never realistically meet the clearinterval statement in a user session. The future date variable im adding to the function is a dynamic unix timestamp from PHP for a date which is roughly 40 days in future. Currently set to 1444761301.88

function MModeTimer(futureDate) {
    zIntervalActive = true;
    var currentTime = new Date().getTime() / 1000;
    var timeRemaining = futureDate - currentTime;

    var minute = 60;
    var hour = 60 * 60;
    var day = 60 * 60 * 24;
    var zDays = Math.floor(timeRemaining / day);
    var zHours = Math.floor((timeRemaining - zDays * day) / hour);
    var zMinutes = Math.floor((timeRemaining - zDays * day - zHours * hour) / minute);
    var zSeconds = Math.floor((timeRemaining - zDays * day - zHours * hour - zMinutes * minute));

    if (zSeconds <= 0 && zMinutes <= 0) {
        console.log("timer in negative");
        // timer at zero 
        clearInterval(zTimeInterval);

    } else {

        if (futureDate > currentTime) {
            console.log("timer interval running");
            // changes html as part of function
        }

    }


}

zTimeInterval = setInterval(function() {
    MModeTimer(zNewTime)
}, 1000);
Ash
  • 424
  • 6
  • 26
  • Does the console log out `timer in negative` before it stops working? – Dom Aug 27 '15 at 22:45
  • no, (that would be too easy! ;) ) – Ash Aug 27 '15 at 22:45
  • check the filters in the console :p perhaps you turned off the logs and only have error activated ;) – taxicala Aug 27 '15 at 22:46
  • Does it just stop logging? Then `futureDate <= currentTime` – Jan Aug 27 '15 at 22:46
  • It's a built in browser function – Dr.Molle Aug 27 '15 at 22:47
  • @RobG sorry ive made an edit that explains this – Ash Aug 27 '15 at 22:48
  • Remove the call to `clearInterval`. Does the observed problem persist? If not then it's a logical error (and "find the bug in my code"); if it still persists, however, then it's not related to the `clearInterval` and such can be eliminated when making a SSCCE. – user2864740 Aug 27 '15 at 22:48
  • @taxicala hah, no sorry its not that – Ash Aug 27 '15 at 22:48
  • @Jan it stops logging but does not give the end statement, it cant have reached the clearinterval part of the function.. ive had an edit to explain this – Ash Aug 27 '15 at 22:49
  • @Dr.Molle could you expand a bit? – Ash Aug 27 '15 at 22:50
  • see http://stackoverflow.com/questions/6737067/jquery-setinterval-too-fast-when-coming-from-another-tab/6737154#6737154 – Dr.Molle Aug 27 '15 at 22:50
  • @Dr.Molle That requires that the tab/windows is in the background and, AFAIK, it won't stop the interval entirely (which differs from something like the RAF).. – user2864740 Aug 27 '15 at 22:51
  • 1
    It doesn't need to hit `clearInterval`. You have a conditional saying that it should only log until `futuretime` has passed. So my guess is you aren't setting `futuretime` to what you think you're setting it to. Show how you're setting it. – Jan Aug 27 '15 at 22:51
  • 1
    It's very likely that since you're calling it with an interval of 1000, it isn't running exactly on each second so the condition `(zSeconds <= 0 && zMinutes <= 0)`, which should be true once every minute, is only true from time to time, more or less randomly. So the timer runs for a while, then stops when both minutes and seconds happen to be zero. Change the condition to `if (timeRemaining <= 0) clearInterval(zTimeInterval)`. – RobG Aug 27 '15 at 22:53
  • @user2864740: it isn't clear if the window was in the foreground(usually you didn't care if a browser-window does have the focus when you leave the PC, there also may be other applications that got the focus) – Dr.Molle Aug 27 '15 at 22:55
  • @RobG ahhh! thats genius... of course, thats it. I never even considered looking at that part. Ill try that in the morning but im sure thats correct, makes perfect sense – Ash Aug 27 '15 at 22:58
  • @Jan its taking a unix timestamp of 1444761301.88, so roughly 40 days in the future. I think RobG has figured it out.. I did this part of the program ages ago and completely forgot to change the conditional – Ash Aug 27 '15 at 23:00
  • 1
    It makes sense because then it would stop every hour on the hour. The curious part is if it never logs the "clearing" message... – Jan Aug 27 '15 at 23:02

1 Answers1

2

This line:

clearInterval(zTimeInterval);

Is clearing the interval when the condition:

if (zSeconds <= 0 && zMinutes <= 0) {

Is met.

And as per the log you've wrote inside, that would be wrong. You are checking that zSeconds and zMinues are less or equal to 0. So when both are 0, the interval will be cleared.

Edit

As per your edits and explanations, may I suggest adding a console log that i'ts not inside any condition?:

function MModeTimer(futureDate) {
    console.log('running');
    //... rest of your code

That way you can make sure if the interval is running, maybe your conditions are not being TRUE after a while and you won't see any log, but the interval would be still running.

taxicala
  • 21,408
  • 7
  • 37
  • 66
  • sorry missed a vital part of my desciption, timer is set to run for around 40 days. The clearinterval statement will never be true in a user session (well not for a while anyway!) – Ash Aug 27 '15 at 22:47