Click here to Skip to main content
15,915,702 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I have javascript codes in which I will make a function call every 5 minutes interval. But one thing I am not sure is that, if the time I run the program is let say 12.13pm, then I want to call the function at 12.15pm then the subsequent 5 minutes, meaning 12.20pm, 12.25pm......

I have a javascript which looks like below:
JavaScript
window.setInterval(function ()
{
    MyMethod();
}, 300000);


How can I code for the 12.15pm part?
Any help would be appreciated. Thank you.

What I have tried:

Research online for solution but no avail.
Posted
Updated 3-Jun-16 0:15am
Comments
Mehdi Gholam 3-Jun-16 5:37am    
Run your timer every 1 minute and check the clock for 5 minutes passed then run your method.
Jamie888 3-Jun-16 7:34am    
Sir, how do you mean by run the timer for every 1 minute?
Sergey Alexandrovich Kryukov 3-Jun-16 10:47am    
First of all, I would question why do you do that repeated call. What is the purpose?
—SA
Sergey Alexandrovich Kryukov 3-Jun-16 10:48am    
First of all, I would question why do you do that repeated call. What is the purpose?
Maybe you rather lack such thing as server push...
—SA

1 solution

Refer Below Code

JavaScript
window.setInterval(function () {
       var date = new Date();
       alert(date.getMinutes());
       if ((date.getMinutes() % 5) == 0) {

           MyMethod(); // your method
       }
   }, 60000);



Check Multiple of 5 Minute's and run your method " MyMethod();" on a setInterval of 1 Minute.
 
Share this answer
 
v3
Comments
Jamie888 5-Jun-16 7:20am    
Sir, I have tried your method but with some modification on the script. It seems did not refresh on what I have expected. Below are my codes, is there anything wrong with it?
if (_clearTimer == 0) {
_clearTimer = setInterval(function () {
if ((_date.getMinutes() % 5) == 0) {
MyMethod();
}
}, 60000);
}
Jamie888 5-Jun-16 7:29am    
Is there any logic break at the modulus part? As if for the first time the result of modulus is not zero, then the setInterval will run nothing inside it and subsequently nothing will run at 5 minutes interval mark. I have tried to change the modulus to 1 and it works, but 5 id did not work.
Jamie888 5-Jun-16 8:14am    
Ok sir, I have found out the root cause. It was my misunderstanding on the setInterval logic. Basically we got to declare a local variable instead of global one(var date in this case). If it is a local, then every interval there would be one new var date being created and each time the getMinutes() would have different value. If it is global, then it would have the same getMinutes() value for every interval and hence the if statement would not stand as it will be always the same. Please correct me if I have explained it wrong here.
And thousands thanks for your help nd effort. It really saved my day sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900