在html元素中顺序运行的事件参数


// Send AJAX request to route
function fetch_post(key, ...values){
const formData = new FormData();
for (let value of values){
formData.append(key, value)
}
fetch('/settings', {
method: 'post',
body: formData
});
}

在这个属性值中:

onchange="fetch_post(this.name, this.value); location.reload();"

我想在fetch_post运行结束时运行location.reload()

您希望location.reload()在fetch_post运行完成时运行。

then为承诺链。

如果fetch被成功调用,代码将被执行。


fetch('/settings', {
method: 'post',
body: formData
})
.then((res) => {
console.log(res);
location.reload();
});

只需在location.reload()中添加这样的fetch函数:

// Send AJAX request to route
function fetch_post(key, ...values){
const formData = new FormData();
for (let value of values){
formData.append(key, value)
}
fetch('/settings', {
method: 'post',
body: formData
}).then((data) => {
location.reload(); <----- this
}).catch((err)=> {
}).finally(() => {
});
}

希望这有帮助!

最新更新