繁体   English   中英

捕获javascript“输入意外结束”

[英]catching javascript “unexpected end of input”

这是Rails应用程序中的视图-不幸的是,我遇到了无法修复的错误(“未捕获的语法错误:输入的意外结束”)。 尽管我确信答案很简单,但要添加一个额外的}); 无法解决问题。

有人对我可能忘记了将某件事关闭的想法有什么想法?

谢谢!

在HAML中:

- content_for :before_head do
  = javascript_include_tag "http://maps.google.com/maps/api/js?sensor=false"


- content_for :page_javascript do
  = javascript_include_tag "http://www.mapnificent.net/media/api/1/mapnificent.js"
  :javascript
    var MAP, HEATMAP, DATAPOINTS;
    var mapnificent, urbanDistance, positions = {};
    $(document).ready(function() {
      DATAPOINTS = {};
      var myLatlng = new google.maps.LatLng(40.75297891717686, -73.93936157226562);

      var myOptions = {
            zoom: 12,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            disableDefaultUI: true,
            scrollwheel: true,
            draggable: true,
            navigationControl: true,
            mapTypeControl: false,
            scaleControl: true,
            disableDoubleClickZoom: false,
            streetViewControl: false,
            mapTypeControl: false,
            overviewMapControlOptions: false,
            styles: [
                  {
                    featureType: "administrative",
                    elementType: "geometry",
                    stylers: [
                      { visibility: "off" }
                    ]
                  },{
                    featureType: "administrative.locality",
                    stylers: [
                      { visibility: "on" }
                    ]
                  },{
                    featureType: "transit.line",
                    stylers: [
                      { visibility: "off" }
                    ]
                  },{
                    elementType: "geometry",
                    stylers: [
                      { saturation: -100 },
                      { lightness: -100 },
                      {gamma:0.24}
                    ]   
                  },{
                    featureType: "road.arterial",
                    elementType:"labels",
                    stylers: [
                      { visibility: "simplified" },
            //        { saturation: -100 },
            //        {lightness:70},
            //        {hue: "#ffc243"}
                    ]
                  },{
                    featureType: "road.highway",
                    elementType: "labels",
                    stylers: [
                      { visibility: "simplified" },
            //        { saturation: -100 },
            //        {lightness:70},
            //        {gamma: 0},
                    ]   
                  },{
                    featureType: "landscape",
                    elementType: "geometry",
                    stylers: [
                      { lightness: 92 },
                      { saturation: 20 },
                      { hue: "#e2ded3" },
                    ]
                  },{
                    featureType: "road",
                    elementType: "geometry",
                    stylers: [
                      { visibility: "simplified"},
                      { saturation: 10 },
                      { lightness: 100 },
                      { hue: "#ffc243" }
                    ]
                  },{
                    featureType: "water",
                    stylers: [
                      { visibility: "simplified" },
                      { lightness: 70 },
                      { saturation: 15 },
                      { hue: "#00ffdd" }
                    ]
                  },{
                    featureType: "poi",
                    elementType: "geometry",
                    stylers: [
                      { visibility: "on" },
                      { saturation: 15 },
                      { lightness: 89 },
                      { hue: "#e2ded3" }
                    ]
                  },{
                    featureType: "transit.station",
                    elementType: "geometry",
                    stylers: [
                      { visibility: "simplified" },
                      { lightness: 85 },
                      { hue: "#e2ded3" },
                      { saturation: 15 }
                    ]
                  },
                  {
                    featureType: "poi.park",
                    elementType: "geometry",
                    stylers: [
                      { hue: "#61bb00" },
                      { lightness: -15},
                      { saturation: 20},
                      { visibility: "simplified" }
                    ]
                  },{
                    featureType: "transit.station",
                    elementType: "labels",
                    stylers: [
                      { hue: "#06bca9" },
                      { saturation: -30},
                      { lightness: -10},
                      { visibility: "on" }
                    ]
                  }
                ]

            };

      MAP = new google.maps.Map(document.getElementById("map"), myOptions);








      /* 
       This is the UrbanDistance's (i.e. the public transport layer) UI function.
       This function gets called during the setup process and you can easily setup your UI in there 
       Parameters:
          mapnificent: the mapnificent instance, responsible for how to draw on the map
          that: the urbanDistance layer object, responsible for handling the public transport travel time calculation and drawing
          $: jQuery for your convenience, scoped to this function, shortcut to window
          undefined: the real undefined value, only 4 arguments are passed

      */

      var UrbanDistanceUI = function(mapnificent, that, $, window, undefined){
          /* that.bind binds a function to an event of the layer 
              setup is called when the setup of the layer is complete
              and before the loading of necessary data. 
          */
          that.bind("setup", function(){
              /* There are many options for the public transport layer 
              They can be read with .getOption and set with .setOption */
              var color = that.getOption("color");
              that.setOption("color", true);
              /* you must display this copyright notice somewhere on your page */
              $("#copyright").html(that.getOption("copyright"));
              console.log("setup done");
          });


          /* 
          loadProgress binds to the data loading progress,
          you will receive one parameter: a percentage */
          that.bind("loadProgress", function(progress){

              $('#loading').text(progress);
              if(progress >= 100){
                  $('#loading').text("Almost done...");
              }
          });

          /*
          fires when the data has finished loading */
          that.bind("dataLoaded", function(){
              $('#loading').text("Done!");
              var geopos = {lat:40.75297891717686,lng:-73.93936157226562}, time = 30 * 60;
              /* adds a position to the map at the specificed location.
              Time can be left out for the default time (15 minutes) */
              var pos = that.addPosition(geopos, time); // time in seconds!
              /* The call returns a position object that you can store.
              You can store it by it's index pos.index */
              positions[pos.index] = {position: pos, latlng: geopos};
              /* You only created a position, you need to start the
               calculation process explicitly */
              pos.startCalculation();
          });

          /* fires when a calculation has started, receives position as parameter */
          that.bind("calculationStarted", function(position){
              $('#loading').text("Starting calculation: 0.0%");
          });

          /* fires when there is a calculation progress, receives position as parameter */
          that.bind("calculationUpdated", function(position){
              /* estimating the progress is not an exact since 
              the option value "estimatedMaxCalculateCalls" is 
              an estimated upper bound of position.calculationProgress
              */
              var percent = position.calculationProgress /
                  that.getOption("estimatedMaxCalculateCalls") * 100;
              $('#loading').text("Calculating: "+percent+"%");
          });

          /*fires when the calculation is done */
          that.bind("calculationDone", function(position){
              $('#loading').text("Calculation Done!");
              console.log("done");
              /* you need to trigger a redraw of the canvas yourself at appropriate moments */
              mapnificent.trigger("redraw");
              /* see getBlobs function further down */
              getBlobs();
          });

          var movePosition = function(index, pos){
              /* you can move your position with the .move method of a position
              it takes an geo object {lat: X, lng:Y} as first parameter.
              last parameter indicates if positionMoved should be triggered */
              var result = positions[index].position.move(pos, true);
              /* result indicates if the position is in range of the current city data
                 if it is not, positionMoved has not been triggered, regardless of second parameter
                 it it is true, postionMoved has been triggered (it already executed)
                 the latlng field of the position is set regardless of anything
                 */
              if (!result){
                  console.log("moved outside of range!");
              }
          };
          /* triggered when a position moved inside the range */
          that.bind("positionMoved", function(pos){
              positions[pos.index].latlng = pos.latlng;
              console.log("position move to ", pos.latlng);
          });

          var removePosition = function(index){
              /* you can call removePosition on the UB layer with an index 
              to remove this position. This will also stop all ongoing calculations */
              that.removePosition(index);
          };

          /* fires after successful removal */
          that.bind("positionRemoved", function(index){
              console.log("position removed");
              delete startPositions[index];
              mapnificent.trigger("redraw");
          });

          var getBlobs = function(){
              /* A blob is a currently visible*, highlighted area that is 
              separate from other highlighted areas. The blob contains the 
              maxbounds in canvas coordinates, and some other information */

              var blobs = that.search.detectBlobs();
              console.log(blobs);
          };

      /* tried adding in a '}' here */    


















      google.maps.event.addListenerOnce(MAP, "idle", function() {
        HEATMAP = new HeatmapOverlay(MAP, {
            "radius": 30,
            "visible": true,
            "opacity": 60
        });

        getDataPoints("theater", "http://doug.cartodb.com/api/v2/sql/?q=SELECT ST_x(geom) AS lng, ST_y(geom) AS lat, count(*) FROM (SELECT ST_SnapToGrid(the_geom,0.005) AS geom FROM v_theaters) AS foo GROUP BY lng, lat");
        getDataPoints("noise", "http://doug.cartodb.com/api/v2/sql/?q=SELECT ST_x(geom) AS lng, ST_y(geom) AS lat, count(*) FROM (SELECT ST_SnapToGrid(the_geom,0.005) AS geom FROM noise) AS foo GROUP BY lng, lat");
        getDataPoints("4sqDrug", "http://doug.cartodb.com/api/v2/sql/?q=SELECT ST_x(geom) AS lng, ST_y(geom) AS lat, count(*) FROM (SELECT ST_SnapToGrid(the_geom,0.005) AS geom FROM _4sq_15_32_47_drugstores_20or_20pharmacies) AS foo GROUP BY lng, lat");        
       });





       if(typeof Mapnificent !== "undefined" && Mapnificent.isBrowserSupported()){
           /* load Mapnificent options for these coordinates */
           Mapnificent.forCoordinates({lat:40.75297891717686,lng:-73.93936157226562}, function(options){
               if(options == null){
                   /* if null, there is no Mapnificent data for this point */
                   return;
               }
               /* if there are options, you can instantiate a new Mapnificent instance */
               mapnificent = Mapnificent(options);
               /* add the "urbanDistance" layer with the UI function */
               mapnificent.addLayer("urbanDistance", UrbanDistanceUI);
               /* you can bind to the initDone event of mapnificent */
               mapnificent.bind("initDone", function(){
                   // mapnificent.hide();
               });
               /* finally add mapnificent to your map by passing the Google Maps instance */
               mapnificent.addToMap(map);
           });
       }





      /* tried removing a "});" from two lines below this comment */

    });

    function showHeatmap() {
      var max = 0;
      var all_points = [];
      for(var key in DATAPOINTS ) {
        DATAPOINTS[key].rows.forEach(function(row) {
          if (row.count > max) {
            max = row.count
          }
        });
        all_points = all_points.concat(DATAPOINTS[key].rows);
      }

      var testData = {
        max: max,
        data: all_points
      }

      HEATMAP.setDataSet(testData);
    }

    function getDataPoints(name, url) {
      $.get(url, function(data) {
        DATAPOINTS[name] = data;
        showHeatmap();
      });
    }

%b
  #loading
  #copyright

错误出现在下一个代码块之前-错误语法:输入意外结束

.map_holder{:style => "float: left; width: 600px; height: 600px;"}
  #map{:style => "width: 100%; height: 100%; border: 1px solid #333;"}  
.side_bar{:style => "float: right;"}
  %p SideBar
  = label_tag "Noise Data"
  = check_box_tag "Test"
.cl{:style => "clear: both;"}

通过使用console.log(),我认为您现在不在IE中进行测试。 但是,如果错误出在IE中,我将从行中删除尾随逗号:

{ visibility: "simplified" },

在这种情况下,我所知道的最佳调试技术是进行“二进制搜索”。 删除一半的代码(当然,不要只是将其砍掉而不会留下不匹配的括号或花括号),尝试在浏览器中运行它,看看是否仍然存在语法错误。 重复直到将其范围缩小到一小段代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM