gpt4 book ai didi

javascript - 在 jQuery 延迟后加载 jquery 插件文件

转载 作者:行者123 更新时间:2023-11-30 20:02:37 25 4
gpt4 key购买 nike

我有一个 js 文件,我想在 Jquery 延迟函数之后加载它。事情是我将数据带入我的 getData 函数,然后将字符串注入(inject) DOM。之后,js 文件将初始化该代码。这就是我想在代码运行后调用 js 文件的原因

我该怎么做

下面是我的代码。

jQuery function decalaration

function getData(url) {
return $.ajax({
url: url,
method: 'GET',
headers: {
Accept: 'application/json; odata=verbose',
},
})
}

function call

getData(url).then(function(data){

});

现在我想在 finish of then 函数之后调用 jQuery 文件。

<script src="file.js"></script>

最佳答案

你需要使用 Promise/await 方式,因为你需要在执行完一些东西后写 js 文件,这样你就可以让 js 文件等到你的代码完成,然后将你的新 js 文件写入 body....检查这个例子:

/* MEthod used to write js to body script */
function addJsFileToBody(jsSrc)
{
var script = document.createElement("script");
script.src = jsSrc;
document.getElementsByTagName("body")[0].appendChild(script);
}

var promise1 = new Promise(function(resolve, reject) {
$.ajax({
url: url,
method: 'GET',
headers: {
Accept: 'application/json; odata=verbose',
},
complete: function(data) {
resolve(data);
}
})
});

promise1.then(function(value) {
addJsFileToBody('file.js');
});

引用和例子: Promiseawait

关于javascript - 在 jQuery 延迟后加载 jquery 插件文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53209438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com