I sincerely thanks your help with my question and had solved my problem.
This forum is lucky to have a person like yourself.
tags. //Declare this at the global scope var myGlobalVariable; function countfeat(type,select) { var selectedOption=select.options[select.selectedIndex]; query = new esri.tasks.Query(); query.where = type + " Like '%" + selectedOption.value + "%'"; queryTask = new esri.tasks.QueryTask("http://vdid-gisedn-2:8399/arcgis/rest/services/flow_BRID/MapServer/0"); queryTask.executeForCount(query, function(count){ alert(count); //it works here with a correct number returned //Yes, and this is where you assign the value to your variable, but you can only use the value AFTER this has been done alert("myGlobalVariable = " + myGlobalVariable); //Will have a value of undefined the first time this runs, thereafter will have the previous value...unless you clear it between calls myGlobalVariable = count; alert("myGlobalVariable = " + myGlobalVariable); //Will now have the value of count }); }; //Hook this function up to a button, it will allow you to check the value of the global variable function testValue(){ alert("myGlobalVariable = " + myGlobalVariable); //This will show undefined until the callback of executeForCount has run }
console.log("Declaring variable 'number'"); var number; //global variable declared console.log("Value of number = " + number); function countfeat(type,select) { console.log("countfeat called, number = " + number); var selectedOption=select.options[select.selectedIndex]; query = new esri.tasks.Query(); query.where = type + " Like '%" + selectedOption.value + "%'"; queryTask = new esri.tasks.QueryTask("http://vdid-gisedn-2:8399/arcgis/rest/services/flow_BRID/MapServer/0"); console.log("Before executeForCount, number = " + number); queryTask.executeForCount(query, function(count){ console.log("executeForCount callback called with count = " + count + ", number = " + number); number = count; //assign it to the global variable console.log("Finished executeForCount callback, value of number is now = " + number); }); console.log("After calling executeForCount, number = " + number); console.log("countfeat finished, number = " + number); }; //If you've just this code written in the global scope then it will execute immediately, definitely before you've even called your method above! if (number > 0){ alert(number); }
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.