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
}
}
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
警报所做的一件事是它会产生延迟,因为您必须单击该按钮。如果警报后的代码依赖于已完成的 ajax 调用,则 ajax 可能会及时完成,因为代码被警报延迟了,amirite?
如果没有警报,此方案将失败
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?
This scenario will fail if there is no alert