C. Kelly C. Kelly - 6 months ago 17
jQuery Question

Delay function call

My problem:
I have two js files, the first file being loaded is calling functions that are on the second file before it's loaded. As a result I am getting an error that says "is not a function" an rightfully so because it does not exist yet.

My Question:
Is there a way to delay the function call until the second file is loaded?

My attempt:

$.getScript("manualEntryCalculations.js", function(){
hPFt();
});

var hPFt = function() {
var val = hpDim(); //H25
var val2 = speedRad1(); //H21
var val3 = dift(); //H20
var val4 = Math.pow(val2, 2);
var val5 = val * val4;
var val6 = Math.pow(val3, 2);
var total = (val5 * val6) / 32.2 / 100;
$('#hPFt').val(+total.toFixed(2));
return total;
};
hPFt();


I tried this but encountered another issue an got the error "hPFt();" is not a function.

Any help would be appreciated

Answer

try this:

$.getScript( "manualEntryCalculations.js" ).done(function() {
  function hPFt(){
      var val = hpDim(); //H25
      var val2 = speedRad1(); //H21
      var val3 = dift(); //H20
      var val4 = Math.pow(val2, 2);
      var val5 = val * val4;
      var val6 = Math.pow(val3, 2);
      var total = (val5 * val6) / 32.2 / 100;
      $('#hPFt').val(+total.toFixed(2));
      return total;
  };

  hPFt();
});

So hPFt() function will be executed after manualEntryCalculations.js is completly loaded.

Comments