javascript - recursive method making issue in events -


if call test(3) i'm getting alert message 1.

function test(param) {      var tt = [ "a", "b", "c" ];     ( var = 0; < param; i++) {         if (tt[i] == "b") {             test(1);             alert(i);         }     } } 

but it's not working in success event. if call below method test(3) , pls consider it's success request. getting alert message 3.

function test(param) {      var tt = [ "a", "b", "c" ];     ( var = 0; < param; i++) {         if (tt[i] == "b") {             ext.ajax.request( {                 url : 'test.do',                 method : 'post',                 success : function(response) {                     test(1);                     alert(i);                 }             });         }     } } 

you need put i in closure around asynchronous function call. can realize using iife, creates scope inside for-loop, in value of i preserved:

for ( var = 0; < param; i++) {     if (tt[i] == "b") {         (function(i){             ext.ajax.request( {                 url : 'test.do',                 method : 'post',                 success : function(response) {                     test(1);                     alert(i);                 }             });         }(i));     } } 

but looks like, need i alert-message debugging purposes only. if else works fine , don't need access i inside of callback function (success), don't need additional scope.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -