Cannot use javascript function after script loaded using jQuery -
i'm trying programmatically load local javascript file - papaparse library, , use 1 of functions:
$.getscript("./content/scripts/papaparse.js", function () { console.log("papaparse loaded successfully"); papa.parse(file, { skipemptylines: true, header: false, complete: completecallback }); });
the script loaded successfully, calling parse method throws error:
referenceerror: papa not defined
within papaparse library, papa defined follows:
(function (global) { "use strict"; var papa = {}; papa.parse = somefunction; . . global.papa = papa; }
if helps, entire code called typescript file.
doing wrong?
as castro pointed out in answer here according offical documentation of jquery's getscript
the callback of getscript method fired once script has been loaded not executed.
that means when getscript
's callback function called target script being loaded in current page context not executed need give time javascript engine execute script. how give time. hmm 1 of options settimeout/setinterval
.
you use settimeout/setinterval
right inside callback function of getscript.
modified version of code :-
$.getscript("./content/scripts/papaparse.js", function () { console.log("papaparse loaded successfully"); function dealwithpapa() { papa.parse(file, { skipemptylines: true, header: false, complete: completecallback }); } //regularly check after 100ms whether papa loaded or not var interval = setinterval(function() { if(papa !== undefined) { //once have reference papa clear interval clearinterval(interval); dealwithpapa(); } },100); });
i hope clear doubt.
Comments
Post a Comment