I have a question that I just cant seem to find out how to do -
I've seen many clientside countdown plugins and I have one on my app - however I'm interested in counting to a certain date on my server, and once that date and time has arrived, I want to trigger a response to the client with a popup page.
Now - I have tried doing that on the clientside with a countdown.onFinish function. But from how I understand this, it will not work unless the client is in the app ON the time of the countdown finish. Am I correct in assuming that the client script is only generated when a client enters the app? Therefor when the user comes in, the timer would have restarted already (On Login I update the timer to countdown to the nearest monday at 21:00).
On the Server - I can check if the date has arrived, outside a function, but this would only happen once on deploy, correct? and if I check it on some kind of intervals, every time I update the app these intervals would get messed up and I might miss the 00:00 hour.
This is the idea of what I have on my serveside (Using expressJS, nodeJS)
var today = new Date();
var currentDay = today.getDay() + 1;
var currentMonth = today.getMonth() + 1;
var currentDate = today.getDate();
var currentYear = today.getFullYear();
//var date = [02, 22, 2014];
var dateTillMatch = findNearestMonday(currentDay, currentDate, currentMonth, currentYear);
if (dateTillMatch[0] == currentDay && dateTillMatch[1] == currentDate && dateTillMatch[2] == currentMonth && dateTillMatch[3] == currentYear) {
updateAllStats();
}
and this is what I had on my clientside - which did not work - because at the time of 00:00 none of my users were inside the app! (genius I know..)
function timer(date) {
$('#clock').countdown(date[2] + '/' + date[0] + '/' + date[1] + ' 21:30:00', function(event) {
$(this).html(event.strftime('%D days %H:%M:%S'));
}).on('finish.countdown', function(event) {
var played = localStorage.getItem('attending');
//If the player was attending/not attending at end of countdown, send to DB that GamesPlayed ++ or gamesNotPlayed ++
if (played == 1 || played == -1) {
var request = false;
var result = null;
var name = localStorage.getItem('first') + " " + localStorage.getItem('last');
request = new XMLHttpRequest();
if (request) {
request.open("GET", "usersGamesPlayed/" + name + "_" + played);
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
result = JSON.parse(request.responseText);
if (result == null) {
alert('PROBLEMO');
} else {
if (played == 1) {
localStorage.setItem('gamesPlayed', parseInt(result.gamesPlayed));
} else {
localStorage.setItem('gamesNotPlayed', parseInt(result.gamesNotPlayed));
}
//Set back to 0
localStorage.setItem('attending', 0);
//Popup to find Best Player
console.log("Choose MVP");
$('#bestPlayerPopup').popup();
$("#bestPlayerPopup").popup("open");
}
}
};
request.send(null);
}
}
});
}
So to conclude - I understand that its pointless to wait for the clientside counter to hit 00:00 because "If a tree falls and no one is there to hear it, does it make a sound?" - or something :)
So how do I do the same on the server? AND - on the end of time, how do I send the user a popup - or atleast redirect him to a page. Will I need to bind it to some request from the client? because than it defeats the purpose of doing it on 00:00.
My DB is MongoDB so I can use that if necessary.
I hope the question isn't too long!
Thank you