【发布时间】:2011-11-08 19:55:34
【问题描述】:
以下内容让我非常困惑。正如 cmets 中所指出的,这些比较似乎可以单独进行,但是放在一起时却不能
while 应该在同一个月的所有日子里运行,然后将 i 加一,然后重新开始。
我已将整个序列与 console.log 结合起来试图弄清楚,但这没有任何意义。一切似乎都彼此相等,但仍然未能通过 while 语句中的“==”测试。
var i=0;
var currentdate = 0;
var currentmonth = 0;
var opensmonth = 0;
var opens = [
{ "date":"3/30/2006","zip":"30038","latitude":"33.676358","longitude":"-84.15381"},
{ "date":"4/31/2006","zip":"30519","latitude":"34.089419","longitude":"-83.94701"}
];
intid = setInterval("stepthrough()", 250);
function stepthrough() {
//figure out first date.
if (currentdate == 0) { // we've not been run before
currentdate = opens[0]["date"];
currentmonth = currentdate.split("/", 1);
console.log("Current Month: >" + currentmonth +"<");
}
console.log("Current month: " + currentmonth + " And opensdate: " + opens[i]["date"].split("/", 1));
//
// TWILIGHT ZONE ENTERED.
//
if (currentmonth == 3 ) {
console.log("Current month equals 3."); // PASSES
}
if (opens[i]["date"].split("/", 1) == 3) {
console.log("Opens date equals 3."); // PASSES
}
// BOTH THE ABOVE TESTS PASS IN CHROME AND SAFARI WHAT THE F*$K JAVASCRIPT
while(opens[i]["date"].split("/", 1) == currentmonth) { // WHY DOESNT THIS WORK I HATE COMPUTERS
console.log("Trying to add a point one.");
addpoint(i);
i++;
console.log("Trying to add a point.");
}
//set the date for next iteration
currentdate = opens[i]["date"];
currentmonth = currentdate.split("/", 1);
console.log ("Current date is now: " + currentdate + " and current month is now: " + currentmonth);
jQuery('div#date').text(currentdate);
//if (i>=5000) {
if (!opens[i]["date"]) {
console.log("Clearing interval");
clearInterval(intid);
//jQuery('div#date').text("Limited at 5000 records")
}
}
【问题讨论】:
-
尝试使用
parseInt(stringValue, 10)。 -
如果在
// WHY DOESNT THIS WORK行中使用===会发生什么? This answer 暗示==是邪恶的,不能被信任。 -
老实说,我在这里没有看到任何问题。
-
这与问题无关,但您应该使用
setInterval(stepthrough, 250);而不是setInterval("stepthrough()", 250);。 -
我希望我能投票赞成 @zengr 编辑这个问题的标题,即使在我回答之后我也认为这是一个有争议的陈述(“Javascript 是谎言!谎言!”)
标签: javascript