Classes with async call and callback

by Sascha

HTML

<div id="result">

</div>

CSS

#result {
  background: white;
  color: black;
}

JavaScript

/**
Callback example with asynchronous method call.
*/

var ServiceHandler= function() {
  var self= this;
  
  self.loadData= function(callback) {
  	document.getElementById("result").append("LoadData() called. ");
    window.setTimeout(function() {
      callback();
    }, 4000);
  }
}

PageController= new function() {
  var self= this;
  var _serviceHandler= new ServiceHandler();

  _resumeOperation= function() {
    document.getElementById("result").append("ResumeOperation() called. ");
  }

  _init= function() {
    document.getElementById("result").append("Init() called. ");
    _serviceHandler.loadData(_resumeOperation);
  }
  _init();  
}