javascript - 如何删除javascript中的函数
问题描述
const resumeInfinity = () => {
window.speechSynthesis.resume();
const timeoutResumeInfinity = setTimeout(resumeInfinity, 1000);
console.log(timeoutResumeInfinity);
}
utterThis.onstart = () => {
resumeInfinity();
};
需要 ( resumeInfinity
) 函数在 SpeechSynthesi 之后停止工作
解决方案
为避免错误消息,resumeInfinity is not a function
您不应将其删除,而是将其设置为空函数。但是正如您所定义resumeInfinity
的那样,const
您无法更改该功能。
因此,您可以将其更改为:
let resumeInfinity = () => {
// ... your code
}
然后稍后将其更改为空函数resumeInfinity = () => {}
但是您需要记住,如果该原始函数作为回调传递给其他地方(例如这里setTimeout(resumeInfinity, 1000)
),则此回调仍将引用旧函数。
因此,更好的解决方案是检查该函数是否仍然有效才能执行,否则提前退出。
const resumeInfinity = () => {
if( /* some check if isn't valid to call that function anymore */ ) {
return
}
window.speechSynthesis.resume();
const timeoutResumeInfinity = setTimeout(resumeInfinity, 1000);
console.log(timeoutResumeInfinity);
}
但上述所有这些解决方案实际上只是一种解决方法,因为如果您的应用程序的逻辑是正确的,那么这种情况就永远不会发生。所以需要做这样的事情表明你更有可能需要考虑重组你的代码。
如果只是要停止超时,那么您需要调用clearTimeout(timeoutResumeInfinity)
,并timeoutResumeInfinity
在您知道 SpeechSynthesi 完成的地方提供。
推荐阅读
- pdf - ImageMagick PNG到给定尺寸的PDF
- dataframe - 通过将观察结果移动到新变量来压缩数据框
- javascript - 使用 Yup 的条件 Formik 表单验证
- javascript - 在移动 android 上使用 SWIPER 滚动主页时页面跳转
- python - 用 Python 编程的 Twitter 回复提及机器人工作一次,然后崩溃并出现错误 400:问题是什么?
- java - 实例/对象内的数组:为什么这在 java 上不起作用?
- java - Receiving Java Error - ArrayList$SubList cannot be cast to class
- javascript - How do i call a useReducer dispatch function from another file, which is not a react component? (without using Redux)
- docker - Change entrypoint of a k8s Pod, but keep the CMD
- bpf - Possible to see tracing when using cat or vi opening a text file