添加分钟到while循环

时间:2014-12-02 07:51:40

标签: javascript while-loop

我试图从用户结束时间添加30分钟,然后开启和显示时间

 function() {

            var toTime = new Date(result2);

            while (true) {

                var toTime30 = new Date(toTime.getTime() + toTime.getTimezoneOffset() *30 * 1000);
                var toTime60 = new Date(toTime30.getTime() + 30 * 60 * 1000);

                var addMin = toTime30 + "" + toTime60;


                    alert(addMin);
                    break;

            }

现在代码只在02:00到02:30之间添加第一个,我怎么能这样做呢?它会增加30分钟的时间吗?

更新

  var toTime = new Date(result2);

            while (true) {

                var iCount = 0;
                var toTime30 = new Date(toTime.getTime() + toTime.getTimezoneOffset() *30 * 1000);
                var toTime60 = new Date(toTime30.getTime() + 30 * 60 * 1000);

                var addMin = toTime30 + "" + toTime60 + (++iCount);


                alert(addMin);
                break;
            }

1 个答案:

答案 0 :(得分:0)

初始化之前不能使用变量。你应该做点什么

var iCount = 0;
var t = new Date();
var toTime30 = new Date(t.getTime() + t.getTimezoneOffset() * 30 * 1000);
console.log(t.getTime() + " - " + toTime30.getTime() + " - " +  (iCount++));

.> "1417509086906 - 1417505486906 - 0"