Why is this javascript never executed? -


i have following javascript. want wait until getjson has returned, in flag set, , something. after finding check function below on so, can't log loaded when flag true (and processdata never executed). doing wrong?

        flag = false         $.getjson(<url>,             function(params){                  if (params){                     flag = true;                  }             }         );  var check = function() {         console.log("check a");          if (flag === false) {             console.log('still going check');             return settimeout(200, check);         }         console.log("loaded!");         processdata();     }     check(); 

instead of periodically checking json arrive, can use javascript promise , deferred objects job you. below example.

var url = 'http://www.html5rocks.com/en/tutorials/file/xhr2/';    function loadurl(url) {    var deferredload = $.deferred();    $.get(url, function(data, status) {      if ('success' == status) {        deferredload.resolve(data);      }    });    return deferredload.promise();  }    function documentready() {    var deferredready = $.deferred();    $(document).ready(function() {      deferredready.resolve();    });    return deferredready.promise();  }    $.when(documentready(), loadurl(url)).then(    function(readyrespond, data) {      $('#content').html("loaded page below:" + data);    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="content">


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -