javascript -removal“alert”导致剩余的代码不执行

时间:2011-07-19 14:54:06

标签: javascript alert

我在我的javascript代码中发出警报,用于调试目的。既然代码有效,我想删除警报。我可以毫无问题地删除所有其他警报,但是当我尝试删除此特定警报时,后面的其余代码无法执行。造成这种情况的原因是什么?

参见代码摘录:

var points = [];
alert("declare array");
var bounds = new google.maps.LatLngBounds();
// Calculate the points
// Work around 360 points on circle
for (var i = 0; i < 360; i++) {
    var theta = Math.PI * (i / 16);
    // Calculate next X point 
    circleY = longitude + (cLng * Math.cos(theta));
    // Calculate next Y point 
    circleX = latitude + (cLat * Math.sin(theta));
    // Add point to array 
    var aPoint = new google.maps.LatLng(circleX, circleY);
    points.push(aPoint);
    bounds.extend(aPoint);
}
points.push(points[0]); //to complete circle
var ne = bounds.getNorthEast(); //northeast boundary of rectangular bounds
var sw = bounds.getSouthWest(); //southwest boundary of rectangular bounds
for (var i = 0; i < statesobj.length; i++) {
    for (var j = 0; j < statesobj[i].values.length; j++) {
        if (bounds.contains(statesobj[i].values[j])) {
            var latChange = ((ne.lat() - sw.lat()) / 100);
            var pt2 = new google.maps.LatLng(sw.lat() - latChange, statesobj[i].values[j].lng());
            var intersections = 0;
            for (var l = 1; l < points.length; l++) {
                var seg1 = points[l - 1];
                var seg2 = points[l];
                var latdiff1 = seg2.lat() - seg1.lat();
                var latdiff2 = pt2.lat() - statesobj[i].values[j].lat();
                var londiff1 = seg2.lng() - seg1.lng();
                var londiff2 = pt2.lng() - statesobj[i].values[j].lng();
                if (londiff2 * latdiff1 - latdiff2 * londiff1 != 0) {
                    var segtest1 = (londiff1 * (statesobj[i].values[j].lat() - seg1.lat()) + latdiff1 * (seg1.lng() - statesobj[i].values[j].lng())) / (londiff2 * latdiff1 - latdiff2 * londiff1);
                    var segtest2 = (londiff2 * (seg1.lat() - statesobj[i].values[j].lat()) + latdiff2 * (statesobj[i].values[j].lng() - seg1.lng())) / (latdiff2 * londiff1 - londiff2 * latdiff1);
                    if (segtest1 >= 0 && segtest1 <= 1 && segtest2 >= 0 && segtest2 <= 1) {
                        intersections++;
                    }
                }
            }
            if (intersections % 2 == 1) {
                alert("circle contains: " + statesobj[i].name);
                break; // once find one point of state within a circle don't need to test the rest
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

警报的一件事是它会产生延迟,因为你必须点击frikkin'按钮。如果警报之后的代码依赖于已完成的ajax调用,那么ajax可能会及时完成,因为代码被警报延迟了,amirite?

ajax call (taking 200ms)

alert (takes 1000ms to click)

some more code exepecting ajax to be complete

如果没有警报

,此方案将失败