javascript - Javascript | 在 For 循环之后执行
问题描述
我想在 for 循环动作完成后调用一个函数,请问有什么方向吗?
这是我的代码:
Function1: function () {
var $this = this;
$this.Action();
var l = $this.list.length;
for (var i = 0; i < l; i++) {
Function2(i)
}
function Function2(i){
axios.post('/api', $this.list[i])
.catch(function(error) {
console.log(error);
});
};
},
我想$this.notAction()
什么时候打电话i > l || i == l
解决方案
使用异步/等待
Function1: async function () {
var $this = this;
$this.Action();
var l = $this.list.length;
for (var i = 0; i < l; i++) {
await Function2(i)
}
$this.notAction();
async function Function2(i){
try {
await axios.post('/api', $this.list[i]);
} catch (err) {
console.log(err);
}
};
}
推荐阅读
- java - 从 Java Integer 到 Scala Int 的正确方法:toInt vs unbox?
- php - 在 laravel 6 中以 post 方法发送表单数据给出错误,因为该路由不支持 POST 方法。支持的方法:GET、HEAD
- javascript - gl.attachShader 不是对象
- python - 按钮kivy python的mouse_pos事件
- php - PHP致命错误:未捕获-> Smarty:无法加载模板'file:1'
- python - 手动计算多列的排名平均值
- javascript - ThreeJs不同的材料到立方体的每个面
- icons - Antd 仅在有子项时才展开图标表
- django - 如何在 Python 请求中实际使用 DRF + JWT 身份验证
- javascript - onMouseOver事件时如何停止图像重叠图像?