使用jQuery加载脚本后无法使用javascript函数

时间:2021-01-13 01:19:26

I'm trying to programmatically load a local javascript file - papaparse library, and then use one of its functions:

我正在尝试以编程方式加载本地javascript文件 - papaparse库,然后使用其中一个函数:

$.getScript("./Content/Scripts/papaparse.js", function () {
    console.log("Papaparse loaded successfully");
    Papa.parse(file, { skipEmptyLines: true, header: false, complete: completeCallback });
});

The script loaded successfully, but calling the parse method throws an error:

脚本加载成功,但调用parse方法会引发错误:

ReferenceError: Papa is not defined

ReferenceError:没有定义Papa

Within papaparse library, Papa defined as follows:

在papaparse库中,Papa定义如下:

(function (global) {
    "use strict";

    var Papa = {};
    Papa.parse = someFunction;
    .
    .
    global.Papa = Papa;
}

If that helps, this entire code is called from a typescript file.
What am I doing wrong?

如果这有帮助,则从打字稿文件调用整个代码。我究竟做错了什么?

2 个解决方案

#1


2  

As Castro pointed out in his answer here that according to offical documentation of Jquery's getScript

正如卡斯特罗在他的回答中指出的那样,根据Jquery的getScript的官方文档

The callback of getScript method is fired once the script has been loaded but not necessarily executed.

一旦脚本加载但不一定执行,就会触发getScript方法的回调。

That means when getScript's callback function is called then the target script is only being loaded in current page context not fully executed so you need to give some time to JavaScript engine to execute that script. How could you give that time. Hmm one of the options is setTimeout/setInterval.

这意味着当调用getScript的回调函数时,目标脚本仅在当前页面上下文中被加载而未完全执行,因此您需要花一些时间来使用JavaScript引擎来执行该脚本。你怎么能给那个时间。嗯其中一个选项是setTimeout / setInterval。

You could use setTimeout/setInterval right inside callback function of getScript.

您可以在getScript的回调函数中使用setTimeout / setInterval。

Modified version of your code would look like :-

修改后的代码版本如下: -

$.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 is loaded or not
    var interval = setInterval(function() {
        if(Papa !== undefined) {
            //once we have reference to Papa clear this interval
            clearInterval(interval);
            dealWithPapa();
        }       
    },100);

});

I hope that would clear your doubt.

我希望这会清除你的怀疑。

#2


0  

According to https://api.jquery.com/jquery.getscript/:

根据https://api.jquery.com/jquery.getscript/:

The callback is fired once the script has been loaded but not necessarily executed.

一旦脚本加载但未必执行,则会触发回调。

You might need to use a setTimeout(300, function(){...}) to wait for it to execute.

您可能需要使用setTimeout(300,function(){...})来等待它执行。

#1


2  

As Castro pointed out in his answer here that according to offical documentation of Jquery's getScript

正如卡斯特罗在他的回答中指出的那样,根据Jquery的getScript的官方文档

The callback of getScript method is fired once the script has been loaded but not necessarily executed.

一旦脚本加载但不一定执行,就会触发getScript方法的回调。

That means when getScript's callback function is called then the target script is only being loaded in current page context not fully executed so you need to give some time to JavaScript engine to execute that script. How could you give that time. Hmm one of the options is setTimeout/setInterval.

这意味着当调用getScript的回调函数时,目标脚本仅在当前页面上下文中被加载而未完全执行,因此您需要花一些时间来使用JavaScript引擎来执行该脚本。你怎么能给那个时间。嗯其中一个选项是setTimeout / setInterval。

You could use setTimeout/setInterval right inside callback function of getScript.

您可以在getScript的回调函数中使用setTimeout / setInterval。

Modified version of your code would look like :-

修改后的代码版本如下: -

$.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 is loaded or not
    var interval = setInterval(function() {
        if(Papa !== undefined) {
            //once we have reference to Papa clear this interval
            clearInterval(interval);
            dealWithPapa();
        }       
    },100);

});

I hope that would clear your doubt.

我希望这会清除你的怀疑。

#2


0  

According to https://api.jquery.com/jquery.getscript/:

根据https://api.jquery.com/jquery.getscript/:

The callback is fired once the script has been loaded but not necessarily executed.

一旦脚本加载但未必执行,则会触发回调。

You might need to use a setTimeout(300, function(){...}) to wait for it to execute.

您可能需要使用setTimeout(300,function(){...})来等待它执行。