It's not good to multiply time in the cycle.



  • Trying to multiply time in the cycle, starting with the current:

    <script type="text/javascript">
    var dt = new Date();
    var currentMinutes = dt.getMinutes();
    var currentSeconds = dt.getSeconds();
    for (i = 1; i <= 130; i++) {
        var minutesOfCreating = currentMinutes;
        var secondsOfCreating = currentSeconds*1+i*1;
        if(secondsOfCreating > 59) {
            var secondsOfCreating = secondsOfCreating*1-60*1;
            var minutesOfCreating = minutesOfCreating*1+1*1;
        }
        var minutesOfCreating = minutesOfCreating.toString();
        var secondsOfCreating = secondsOfCreating.toString();
        if(secondsOfCreating.length == 1) {
            var secondsOfCreating = '0'+secondsOfCreating;
        }
        if(minutesOfCreating.length == 1) {
            var minutesOfCreating = '0'+minutesOfCreating;
        }
        console.log(minutesOfCreating+secondsOfCreating);
    }
    </script>
    

    If the second in the cycle is more than 59, then you have to take 60 and add a unit by minute. But as can be seen in the console, this rule only works for the first time, for the second time, it is not possible to minus 60 and thus add a unit by minute.

    What's the nail?


    UPDATING

    Found out before the condition. if(secondsOfCreating > 59) A further condition may be added:

    if(secondsOfCreating > 119) {
        var secondsOfCreating = secondsOfCreating*1-120*1;
        var minutesOfCreating = minutesOfCreating*1+2*1;
    }
    

    Then the problem is solved. But if it's known in advance that there are 130 terations in the entrance, that is, they cover two whole minutes and part three. What if the number of iterations can be any?

    Theoretically, I can take the number of minutes and, accordingly, know how much I need such conditions:

    for (i = 1; i <= range; i++) {
        var number = Math.floor(range/60);
    

    But then how do you feel about the multiplication cycle?



  • I don't understand why you don't want to use built-in language to work with time. ♪ Date The box can make time shifts.

    In the code, it might look like,

    // Эта функция добавляет ведущие нули там, где это нужно.
    var f = function(n) { 
        return (n < 10) ? ('0' + n) : ('' + n);
    };
    

    var dt = new Date();
    for (var i = 1; i <= 130; i++) {
    // Добавляем смещение в одну секунду к временной метке.
    dt = new Date(dt.getTime() + 1000);
    // Выводим результат в формате "минуты":"секунды".
    console.log(f(dt.getMinutes())+':'+f(dt.getSeconds()));
    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2