0

I need to make an ajax request at every 30 minute mark with Javascript.

For the current logged in user I need to retrieve any notifications that have a startdate equal to the current time. When the user creates these notifications they are only allowed to save intervals of 30 minutes. So in the database you will see StartDates like 12/22/2016 08:00:00, 12/22/2016 08:30:00, 12/25/2016 09:00:00, 12/28/2016 09:30:00, etc...

I have moment.js if that will help

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231
  • Sorry this is not possible to do a such exact ajax call. You will have to handle this logic on the server side. You may create a loop which runs every 30 minutes, take a look at this answer: http://stackoverflow.com/questions/3583724/how-do-i-add-a-delay-in-a-javascript-loop – Xavjer Dec 22 '16 at 14:26
  • @Xavjer So I need to set up a recurring job with something like Hangfire and use SignalR to output the notifications to the client? – Blake Rivell Dec 22 '16 at 14:27
  • I do not know, what you want to do exactly, but this would be a possible solution. Why not just store them with the exact date in the db and then retrieve them in packs of 30min (select all 08:00:01-08:30:00 etc.) – Xavjer Dec 22 '16 at 14:28
  • @Xavjer I just want to display a users notifications to them in real time. The notifications are stored in the database with a StartDate. The time on the StartDate is always at a 30 minute mark. At anytime when the user is browsing the site I want to notify them when a notification hits its StartDate. – Blake Rivell Dec 22 '16 at 14:31

2 Answers2

1

You can use a combination of setInterval and setTimeout. If the user refreshes the page it will redo the calculation to the next half hour and go from there. You could utilize the date object getSeconds and getMilliseconds with a similar algorithm to get a higher precision.

var minutes = new Date().getMinutes();
var numberOfMinutesTillNextHalfHour = (minutes <= 30)? 30 - minutes : 60 - minutes;
var minutesToSeconds = 60;
var secondsToMiliseconds = 1000;
setTimeout(function(){
    /* this will execute on the next half hour */
    setInterval(function(){
        /* this will execute ever half hour after that */
    }, 30 * minutesToSeconds * secondsToMiliseconds);
},numberOfMinutesTillNextHalfHour * minutesToSeconds * secondsToMiliseconds);
  • This answer is excellent. And if I wanted to switch it to intervals for every 15 minutes I should be able to with no prob right? Also, I don't think I understand what you are saying to get higher precision. Are you simply saying to update the numberOfMinutesTillNextHalfHour variable to also check seconds and miliseconds? I can easily do moment().get('minute'); moment().get('second'); moment().get('millisecond'); – Blake Rivell Dec 22 '16 at 14:53
0

You could try this

var response = true;
function request(){
    if(response == true){
       response = false;
       var req = $.ajax({
           type:"post",
           url:"URL_CALL",
           data:{data:"Hello World"}
       });
       req.done(function(){
          console.log("Success");
          response = true;
      });
   }
   setTimeout(request(),1000);
}
request();
KevDevMan
  • 808
  • 11
  • 23
  • Would this continuously work regardless of the user changing pages on the site? I am wondering if I could just set a loop that continuously checks the current time and always does an ajax request if the minutes are at 30 or 00. Would this work? – Blake Rivell Dec 22 '16 at 14:32
  • window.globalVar = request() would globalise the function(). should work. – KevDevMan Dec 22 '16 at 14:47
  • Alright, is there anyway you can help me customize the function so it makes less calls. I only need to make the call if the minutes of the current time are at a 15 mark. So couldn't I do something like: if (makeRequest && minute / 15 === 1 || minute / 15 === 2 || minute / 15 === 3 || minute / 15 === 4) Also in your answer it says response isn't defined. Where should I define it? – Blake Rivell Dec 22 '16 at 14:49
  • 1
    Sorry, i dont have time to help you write the whole thing. but i will try to point you in the right direction. first. get the current time. var currentTime = new Date(); var minutes = currentTime.getMinutes(). from minutes you need to get the cloest 15, so i would use a if else to get this. Then i would pass this as arg in the request(minsto15); in success, i would reset time to normal 15 mins. This will never be exact and there will be a loss of time on server etc. – KevDevMan Dec 22 '16 at 14:57
  • I have used this in the past for job scheduling http://www.quartz-scheduler.net/ and it works very will. You are left with the issue of notification to client but this will run a job every x mins for you – KevDevMan Dec 22 '16 at 14:59
  • Yeah I was going to use Hangfire (which is just like quartz) and SignalR, but someone told me this could easily just be done with Javascript. – Blake Rivell Dec 22 '16 at 15:08