Select to view content in your preferred language

How to make the feature count variable global?

3615
7
11-22-2012 12:33 PM
BenLam
by
Emerging Contributor
It may be a simple answer but I need help:
The following works but I need to put the 'count' variable into a global one so that I can use it in other functions.


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){
  dojo.byId('rightPane').innerHTML = count;
  });
};


Thanks
Ben
0 Kudos
7 Replies
__Rich_
Deactivated User
Just declare a variable at the global scope of your page i.e. not inside a function, then assign it the value received by your callback.

Don't be seduced by the dark side of global variables though, there are many pages on the web that will explain why, here's an example:

http://www.w3.org/wiki/JavaScript_best_practices#Avoid_globals

Might be worth you reading up on this area in general:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/var

https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals#Variable_S...

Additionally this:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope
0 Kudos
BenLam
by
Emerging Contributor
Thanks for the reply.
I tried the following but the global variable could not catch the variable "count":



var number;                               //global variable declared

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){
number = count;                       //assign it to the global variable
});
};


if (number > 0){
alert(number);
}

It did not work.
I might have assigned it at a wrong place.  Please help further.
Ben
0 Kudos
__Rich_
Deactivated User
executeForCount is an asynchronous call, your number variable won't be assigned a value until the callback has run.

Perhaps these logging statements will help explain:

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);
}
0 Kudos
BenLam
by
Emerging Contributor
Thanks for your valuable time and reply.  Very much appreciated.

This function is called by users clicking a button somewhere with all the needed variables provided, and is already functional and returned an accurate number answer.


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


But I have no idea how to assign this "count"  value to a variable outside this function so that other scripts can use it.

Thanks
Ben
0 Kudos
__Rich_
Deactivated User
Please wrap your code snippets in
 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
}


I really think you'd benefit from investing some time learning some of the fundamentals of JavaScript (and programming concepts in general) things like scope, closures, asynchronous patterns etc. etc.

Here's a start on the JS stuff:

https://developer.mozilla.org/en-US/docs/JavaScript/Guide
0 Kudos
BenLam
by
Emerging Contributor
I sincerely thanks your help with my question and had solved my problem.
This forum is lucky to have a person like yourself.

Thanks again!

Ben
0 Kudos
__Rich_
Deactivated User
I sincerely thanks your help with my question and had solved my problem.

You're welcome, please mark as answer next to the post that helped you.
This forum is lucky to have a person like yourself.

*blushes*

🙂
0 Kudos