所以,我正在尝试让我的脚本找出学期的相关最后一天。它需要一直运行到当前日期小于(和以前一样)该季度的最后一天。无论如何,它都会继续运行。 ps,我认为问题可能在于它没有重置“lastDayofQuarter”(至少在循环之外)。我怎样才能避免这个?非常感谢!
var today = new Date();
var lastDayofQuarter=new Date();
var lastDaysofQuarter=new Array();
lastDaysofQuarter[0]=(2009,11,3);
lastDaysofQuarter[1]=(2010,11,10);
lastDaysofQuarter[2]=(2011,4,18);
lastDaysofQuarter[3]=(2011,5,10);
lastDayofQuarter.setFullYear(lastDaysofQuarter[0]);
i=0;
while (lastDayofQuarter<today) {
lastDayofQuarter.setFullYear(lastDaysofQuarter[i]);
i++;
}
最佳答案
这些行在语法上是有效的,但它们并没有像您希望的那样工作:
lastDaysofQuarter[0]=(2009, 11, 3);
// ...
comma operator计算它的两个操作数(从左到右)并返回第二个操作数的值。因此 (2009, 11, 3)
只返回 3
。
看起来你想做的事:
lastDaysofQuarter[0] = new Date(2009, 11, 3);
// ...
关于JavaScript 循环无限期地运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3462639/