Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
var StartTime= 8:00;
var EndTime= 17:00;

for ....

I want to add in a loop here 30 minutes every time the loop runs any suggestions?

JavaScript
for (var i = setHours(8); i < toTime.count; i++) {
           toTime.setMinutes(toTime.getMinutes() + 30);

       }



Update
JavaScript
while (true) {
                    // ...
                    var toTime30 = new Date(toTime.getTime() + toTime.getTimezoneOffset() * 30 * 1000);


                    if (true) {
                        // whatever you need to do before leaving the loop 
                        break;  
                    }
Posted
Updated 1-Dec-14 20:56pm
v3

1 solution

If you want to add more time in a loop based on time period, you may end up running infinite loop.

It's possible that you really need to change the break loop condition inside loop though. In this case, you are using a wrong kind of loop. You would need to use the while loop. Alternatively, you use "infinite while" loop by using True condition under while, and, in this case, use break state in the if statement inside loop to break from the loop explicitly, for example:
JavaScript
while (true) {
   // ...
   breakCondition = // some Boolean condition based
                    // on the calculation performed inside loop
   if (breakCondition) {
     // whatever you need to do before leaving the loop 
     break;  
   }
}

Please see, for example: http://www.echoecho.com/javascript9.htm[^].

—SA
 
Share this answer
 
Comments
Kurac1 2-Dec-14 2:36am    
But that loop will not add 30 minutes based on current time? Should i not use a for loop?
Sergey Alexandrovich Kryukov 2-Dec-14 2:49am    
No. You can do all you need depending on condition. Isn't that obvious?
—SA
Kurac1 2-Dec-14 2:56am    
i will update my question, i have made it work so it adds 30 minutes to current time but i want to do is so on.

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