JavaScript没有进入下一个IF声明

时间:2016-11-11 11:31:14

标签: javascript

我无法弄清楚为什么我的程序在我的第一个得分= 0之后不会转移到if语句。当我找到第一个城市的第二个if语句没有触发时,打开我的警告窗口说我们应该找到第二个城市。我不知道为什么得分= 1的if语句没有激活。

<!DOCTYPE html>
<html>

<body>

<div id="map" style="width:100%;height:500px"></div>

<script>

var map;
      var score;
      score = 0;
      function initMap() {
        var chicago = {lat: 41.8781, lng: -87.6298};
        var indianapolis = {lat: 39.7684, lng: -86.1581};
        var oklahomaCity = {lat: 35.4819, lng: -97.5084};
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: 0.0, lng: 0.0},
          zoom: 1
});
        var chicagoMarker = new google.maps.Marker({
            position: chicago,  


});

        var oklahomaCityMarker = new google.maps.Marker({
            position: oklahomaCity,
});

        var indianapolisMarker = new google.maps.Marker({
            position: indianapolis,

});

if (score==0) {
    chicagoMarker.setMap(map);
    chicagoMarker.setVisible(false);
    window.alert("Find my first city!");
    map.addListener('bounds_changed',function(){
        zoom = map.getZoom();
        console.log(zoom);
        if (zoom ==2 && map.getBounds().contains(chicagoMarker.getPosition())){
            window.alert("Good Start!");
        }
        if (zoom ==3 && map.getBounds().contains(chicagoMarker.getPosition())){
            window.alert("Keep Going!");
        }
        if (zoom ==4 && map.getBounds().contains(chicagoMarker.getPosition())){
            window.alert("Getting Closer!");
        }
        if (zoom ==5 && map.getBounds().contains(chicagoMarker.getPosition())){
            window.alert("Closer!");
        }
        if (zoom ==6 && map.getBounds().contains(chicagoMarker.getPosition())){
            window.alert("Keep Going!");
        }
        if (zoom ==7 && map.getBounds().contains(chicagoMarker.getPosition())){
            window.alert("Almost There!");
        }
        if (zoom ==8 && map.getBounds().contains(chicagoMarker.getPosition())){
            window.alert("You found Chicago! Your Score is: 1");
            score = 1;
        }
    })
};


if (score==1) {
    oklahomaCityMarker.setMap(map)
    oklahomaCityMarker.setVisible(false);
    window.alert("Find my second city!");
    map.addListener('bounds_changed',function(){
        zoom = map.getZoom();
        console.log(zoom);
        if (zoom ==2 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
            window.alert("Good Start!");
        }
        if (zoom ==3 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
            window.alert("Keep Going!");
        }
        if (zoom ==4 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
            window.alert("Getting Closer!");
        }
        if (zoom ==5 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
            window.alert("Closer!");
        }
        if (zoom ==6 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
            window.alert("Keep Going!");
        }
        if (zoom ==7 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
            window.alert("Almost There!");
        }
        if (zoom ==8 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
            window.alert("You found Chicago! Your Score is: 1");
            google.maps.event.ClearListeners('bounds_changed');
        }
    })
    score = score + 1;
    google.maps.event.ClearListeners('bounds_changed');

};

if (score==2) {
    indianapolisMarker.setMap(map)
    indianapolisMarker.setVisible(false);
    window.alert("Find my first city!");
    map.addListener('bounds_changed',function(){
        zoom = map.getZoom();
        console.log(zoom);
        if (zoom ==2 && map.getBounds().contains(indianapolisMarker.getPosition())){
            window.alert("Good Start!");
        }
        if (zoom ==3 && map.getBounds().contains(indianapolisMarker.getPosition())){
            window.alert("Keep Going!");
        }
        if (zoom ==4 && map.getBounds().contains(indianapolisMarker.getPosition())){
            window.alert("Getting Closer!");
        }
        if (zoom ==5 && map.getBounds().contains(indianapolisMarker.getPosition())){
            window.alert("Closer!");
        }
        if (zoom ==6 && map.getBounds().contains(indianapolisMarker.getPosition())){
            window.alert("Keep Going!");
        }
        if (zoom ==7 && map.getBounds().contains(indianapolisMarker.getPosition())){
            window.alert("Almost There!");
        }
        if (zoom ==8 && map.getBounds().contains(indianapolisMarker.getPosition())){
            window.alert("You found Chicago! Your Score is: 2");
        }
    })
    score = score + 1;
    google.maps.event.ClearListeners('bounds_changed');

};
    }
</script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCuvsCAF0gVmwv6AF0SoA3xBjV66RG4r7o&callback=initMap"
    async defer></script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

它不会移动到下一个if,因为window.alert语句会冻结程序执行,直到您将其解除为止。请考虑使用非阻塞弹出式组件,例如jQuery-ui dialogBootstrap-ui modals