Home » Javascript » get variable value in callback function

get variable value in callback function

Posted by: admin October 29, 2018 Leave a comment

Questions:

I have one callback function

function QueryKeyword(keyword, site, callback) {
  var querykeyword = keyword;
  var website = site;

  $.ajax({
    url: "http://www.test.com",
    jsonp: "jsonp",
    dataType: "jsonp",
    data: {
      Query: querykeyword
    },
    success: callback
  });
}

I am calling this function with in for loop like this :

for (i = 0; i < questionTerm.length; i++) {
  for (j = 0; j < site.length; j++) {
    var searchTerm = questionTerm[i] + ' ' + $('#search').val();

    QueryKeyword(searchTerm, site[j], function(reslt) {
      // I need to get j variable value here
      console.log(j);
    });

  }

}

Now I need to get “j” variable value in function see I console the j variable value but it does not get the j variable value.

Would you please let me know how I can fetch the value in this.

Thanks in advance

Answers: