javascript - 删除“alert”导致剩余代码不执行

发布于 2024-11-24 21:49:18 字数 2549 浏览 2 评论 0原文

我在 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
            }
        }
    }
}

I placed an alert in my javascript code to use for debugging purposes. Now that the code works I would like to remove the alert. I am able to remove all other alerts without a problem, but when I try to remove this particular alert the rest of the code that follows fails to execute. What is causing this to happen?

see excerpt of code:

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
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初雪 2024-12-01 21:49:18

警报所做的一件事是它会产生延迟,因为您必须单击该按钮。如果警报后的代码依赖于已完成的 ajax 调用,则 ajax 可能会及时完成,因为代码被警报延迟了,amirite?

ajax call (taking 200ms)

alert (takes 1000ms to click)

some more code exepecting ajax to be complete

如果没有警报,此方案将失败

One thing that an alert does is that it creates a delay beacuse you have to click the frikkin' button. If the code after the alert got dependency on a completed ajax call, the ajax might be done in time beacuse the code was delayed by the alert, amirite?

ajax call (taking 200ms)

alert (takes 1000ms to click)

some more code exepecting ajax to be complete

This scenario will fail if there is no alert

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文