• 异步查询query和querytask的陷阱


    双击操作由用户操作系统的鼠标设置确定。用户可以设置两次单击鼠标按钮之间的时间以便将这两次单击认为是双击而不是两次单击。每当双击控件时,就会引发 Click 事件。例如,如果您有Form 的 Click 和 DoubleClick 事件的事件处理程序,则当双击该窗体并同时调用这两个方法时,会引发 Click 和 DoubleClick 事件。如果双击一个控件并且该控件不支持 DoubleClick 事件,则 Click 事件可能被引发两次。关于异步查询的一些陷阱就来源这个doubleclick。

     ——————————————————————————————————————————————————————

    异步缺点和优点:

    1、  高效,用户体验好

    2、  情况变化复杂

    究竟怎么个复杂呢?

     ———————————————————————————————————————————————————————

    queryTask.execute(query); 是查询。

    dojo.connect(queryTask, "onComplete", function(featureSet));是查询成功的异步回调函数。

     dojo.connect(queryTask, "onError", function(err) ); 是查询失败的异步回调函数。

     如果用公共变量来保存查询结果,连续进行两次查询,则可能产生两次查询结果融合的现象,这是由于查询很快执行两次,来不及清空公共变量,查询成功的回调函数稍后执行两次。

    例如:蓝色部分会很快的执行两次 ,而红色部分稍后才执行两次,红色部分是在蓝色执行完两次后才执行,即:蓝蓝红红,而不是红。

      var searchResultContent = "";

       function attQuery(field, value, index, symbol, level, isClear, isLocate, isCatagory) {

     

              var url = "http://meng/ArcGIS/rest/services/StatisticData/MapServer/" + index;

               var queryTask = new esri.tasks.QueryTask(url);

               var query = new esri.tasks.Query();

               query.returnGeometry = true;

               query.outFields = ["zonename","type","year"];

               query.where = field + " Like '%" + value + "%'";

               queryTask.execute(query);


               dojo.connect(queryTask, "onComplete", function(featureSet) {

                   if (isClear == true) {

                       map.graphics.clear();

                   }

                   if (isCatagory == false) {

                       searchResultContent = ""  //会避免查询结果异步返回而导致的searchResultContent未被清空

                       position = 0;

                   }

                   var showXBExtent;

                   searchResultContent = searchResultContent + "<h3>" + level + "</h3>";

                   for (var i = 0, il = featureSet.features.length; i < il; i++) {

                       var graphic = featureSet.features[i];

                       graphic.setSymbol(symbol);

                       graphic.setInfoTemplate(queryInfoTemplate);

                       map.graphics.add(graphic);

                       if (i == 0) {

                           showXBExtent = graphic.geometry.getExtent();

                       }

                       else {

                           showXBExtent = showXBExtent.union(graphic.geometry.getExtent());

                       }

                       searchResultContent = searchResultContent + "<A " +

                       "onclick = lightenGraphic(" + index + "," + position + ");" +   //事件绑定函数中传递参数,如果参数是数字,不用单引号,如果是字符串,要用单引号

                       //"onmouseout = unlightenGraphic();" +

                       "href=\"javascript:void(0)\"; style=\"cursor:pointer\";>" + graphic.attributes["zonename"] + "</A>" + "   ";

                       position = position + 1;

                   }

                   get_ImageStatistics_Infos(searchResultContent);

                   if (isLocate == true) {

                       if (showXBExtent != null) {

                           map.setExtent(showXBExtent.expand(1.5));

                       }

                   }

                   else {

                       fullExtent();

                   }

               });

     

               dojo.connect(queryTask, "onError", function(err) {

                  alert(err.name + ":" + err.message);

                });

     

           }

    ————————————————————————————————————————————————————————————————————————————————

       function attQuery(field, value, index, symbol, level, isClear, isLocate, isCatagory) {

                  if (isClear == true) {

                       map.graphics.clear();

                   }

                   if (isCatagory == false) {

                       searchResultContent = ""  //会产生查询结果异步返回而导致的searchResultContent未被清空

                       position = 0;

                   }

              var url = "http://meng/ArcGIS/rest/services/StatisticData/MapServer/" + index;

               var queryTask = new esri.tasks.QueryTask(url);

               var query = new esri.tasks.Query();

               query.returnGeometry = true;

               query.outFields = ["zonename","type","year"];

               query.where = field + " Like '%" + value + "%'";

               queryTask.execute(query);


              dojo.connect(queryTask, "onComplete", function(featureSet) {

                  var showXBExtent;

                 searchResultContent = searchResultContent + "<h3>" + level + "</h3>";

                   for (var i = 0, il = featureSet.features.length; i < il; i++) {

                       var graphic = featureSet.features[i];

                       graphic.setSymbol(symbol);

                       graphic.setInfoTemplate(queryInfoTemplate);

     

                       map.graphics.add(graphic);

                       if (i == 0) {

                           showXBExtent = graphic.geometry.getExtent();

                       }

                       else {

                           showXBExtent = showXBExtent.union(graphic.geometry.getExtent());

                       }

                       searchResultContent = searchResultContent + "<A " +

                       "onclick = lightenGraphic(" + index + "," + position + ");" +   //事件绑定函数中传递参数,如果参数是数字,不用单引号,如果是字符串,要用单引号

                       //"onmouseout = unlightenGraphic();" +

                       "href=\"javascript:void(0)\"; style=\"cursor:pointer\";>" + graphic.attributes["zonename"] + "</A>" + "   ";

                       position = position + 1;

                   }

                   get_ImageStatistics_Infos(searchResultContent);

                   if (isLocate == true) {

                       if (showXBExtent != null) {

                           map.setExtent(showXBExtent.expand(1.5));

                       }

                   }

                   else {

                       fullExtent();

                   }

               });

               dojo.connect(queryTask, "onError", function(err) {

                  alert(err.name + ":" + err.message);

                });

     

           }

    补充:还有一种解决方案就是每次执行查询后弹出对话框提示正在进行查询,查询成功后弹出对话框提示查询成功,这样就把异步变成了同步。

  • 相关阅读:
    Win7下使用TortoiseGit设置保存密码
    MacOS软件清单
    ubuntu安装python
    Mac使用SSH连接远程服务器
    CentOS常用命令
    Docker追加容器端口映射
    Docker安装CentOS7
    Windows操作路由表
    Docker部署MySQL8并实现远程连接
    Qt——容器类(译)
  • 原文地址:https://www.cnblogs.com/zhangjun1130/p/1991447.html
Copyright © 2020-2023  润新知