![](/img/trans.png)
[英]Why isn't my Javascript function continuously firing with setTimeout/clearTimeout?
[英]Why isn't my settimeout firing inside an imported module?
我有一个 Vue 组件,它这样导入一个模块:
import { sendMsg } from './assets/modules/messages'
sendMsg
看起来像这样(为简洁起见进行了简化)。 有一个setTimeout()
来添加一个人为的(类似人类的)延迟,这样系统就不会立即回复。
export const sendMsg = async msg => {
console.log(1); //happens
await new Promise(res => setTimeout(res, 1000));
console.log(2); //doesn't happen
}
...但是在超时触发后什么也没有,因为超时没有解决。
如果我将所有代码直接移动到组件中,而不是从模块中调用它,它就可以正常工作。 我搜索了“javascript interval not firing inside module”但没有找到任何东西。 我错过了什么?
更新:这仅在被App.vue
调用时发生。 如果我从子组件导入/调用 function,它工作正常......
我认为您还没有阅读有关 javascript 的文档。它是异步的,不会等待事件并继续处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.