trs
trs

Reputation: 2454

javascript -removal of "alert" causes the remaining code not to execute

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

Upvotes: 1

Views: 406

Answers (1)

Emil
Emil

Reputation: 1045

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

Upvotes: 1

Related Questions