js循环条件为执行代码后的变量

怎么样改可以让执行代码在timeteam[n] <= endtime 的条件下执行循环?

        //时间
        var startdate = new Date("2016-10-24 20:00:00");
        var starttime = startdate.getTime();
        var enddate = new Date("2016-10-26 20:00:00");
        var endtime = enddate.getTime();

        var timeteam = new Array();
        var s = 0;
        var sri = "";
        var m;
        var n = 0

        do{
            timeteam[n] = starttime + 1800000*s;
            s++;

            var myDate = new Date(timeteam[n]);

            var m = myDate.getFullYear() + "/" + myDate.getMonth() + "/" + myDate.getDate() + " " + myDate.getHours() + ":" + myDate.getMinutes() + ",";

            sri = sri + m;
            n++;
        }
        while(n < 20)

        var ss = sri.split(",");
        ss.pop();

        console.log(ss);
阅读 2.5k
4 个回答

do{

//....
if(myDate > endtime) {
    break;
}
//....

}while(true);

蟹妖

题主只想在 timeteam[n] <= endtime 的时候执行,也就是 timeteam[n] > endtime 的时候就不执行,于是可以通过 break 跳出题主的那个 while 循环。if (timeteam[n] > endtime) break;

//时间
var startdate = new Date("2016-10-24 20:00:00");
var starttime = startdate.getTime();
var enddate = new Date("2016-10-26 20:00:00");
var endtime = enddate.getTime();

var timeteam = new Array();
var s = 0;
var sri = "";
var m;
var n = 0
var myDate;
do {
    timeteam[n] = starttime + 1800000*s;
    s++;
    if( timeteam[n] > endtime){
        console.log("退出");
        break;
    }
    var myDate = new Date(timeteam[n]);
    // getMonth()  0 = 1月, 1 = 2月
    var m = myDate.getFullYear() + "/" + (myDate.getMonth() + 1) + "/" + myDate.getDate() + " " + myDate.getHours() + ":" + myDate.getMinutes() + ",";
    sri = sri + m;
    n++;
} while( true )

var ss = sri.split(",");
ss.pop();

console.log(ss);

为什么要while(true)呢?直接while(n <20 && timeteam[n] <= endtime) 不就行了吗?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏