jquery - Order of function execution in javascript -


this question has answer here:

i making java script application having html canvas element. clicking on canvas triggers java script event. order of execution of function not expected

$('#canvas1').on('click',function(e){     get_json_data();         //plot json data     show_curve(); });  function get_json_data(){             $.getjson("program/heatmap_slices.json",function(data){                     console.log("function1");         visibilty = data['photo'][1][1];                   });     return ; }  function show_curve(){     $("#canvas2").show();     console.log("function2");     var canvas2 = document.getelementbyid('canvas2');     var ctx2 = canvas2.getcontext("2d");     var mynewchart = new chart(ctx2);      var data = {     labels: points,     datasets: [         {             label: "my first dataset",             fillcolor: "rgba(175,175,220,0.5)",             strokecolor: "rgba(150,150,220,1)",             highlightfill: "rgba(100,100,220,0.75)",             highlightstroke: "rgba(220,220,220,1)",             data: visibilty,         }      ]     };      var options = {         barstrokewidth : 2,     }      var mybarchart = new chart(ctx2).bar(data,options);     points =[];      return; } 

i expect order of console log should "function1 followed function2" other way round. if remove $getjson method, execution order comes expected.

the problem in get_json_data() method call $.getjson() asyncronous method: executes json call , when data arrives, calls callback function. happens that:

  1. you call get_json_data(), calls $.getjson() , takes while
  2. you call show_curve() , prints "function2"
  3. the $.getjson() gets data, calls callback , print "function1".

to solve problem have waits $.getjson() ends before call show_curve(). that, can modify signature of get_json_data() accept callback function call when $.getjson() ends:

function get_json_data(callback){  // <-- callback           $.getjson("program/heatmap_slices.json",function(data){                     console.log("function1");         visibilty = data['photo'][1][1];         callback(); // <-- call inside '$.getjson' callback     });     return ; } 

the have modify intial call in way:

$('#canvas1').on('click',function(e){     get_json_data(function() {         //plot json data         show_curve();     });      }); 

so show_curve() call after get_json_data() ends.

here post code che changes:

$('#canvas1').on('click',function(e){     get_json_data(function() {         //plot json data         show_curve();     });      });  function get_json_data(callback){             $.getjson("program/heatmap_slices.json",function(data){                     console.log("function1");         visibilty = data['photo'][1][1];         callback();     });     return ; }  function show_curve(){     $("#canvas2").show();     console.log("function2");     var canvas2 = document.getelementbyid('canvas2');     var ctx2 = canvas2.getcontext("2d");     var mynewchart = new chart(ctx2);      var data = {     labels: points,     datasets: [         {             label: "my first dataset",             fillcolor: "rgba(175,175,220,0.5)",             strokecolor: "rgba(150,150,220,1)",             highlightfill: "rgba(100,100,220,0.75)",             highlightstroke: "rgba(220,220,220,1)",             data: visibilty,         }      ]     };      var options = {         barstrokewidth : 2,     }      var mybarchart = new chart(ctx2).bar(data,options);     points =[];      return; } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -