javascript - jQuery - 暂时禁用功能然后再次启用它
问题描述
我想禁用功能运行并在特定超时后启用它。
例如,我想在一段时间后停止第一个脚本循环,然后重新启动它。
JS
// Script that loops
function messages() {
setInterval(function() {
setTimeout(function() {
alert("Hello!");
}, 2000);
setTimeout(function() {
alert("I said hello!");
}, 10000);
}, 20000); // It's looping every 20 sec
}
// Script with timeout to stop the loop
setTimeout(function() {
messages.stop();
}, 60000); // Stop it after 60 sec
setTimeout(function() {
messages.start();
}, 120000); // Start again it after 120 sec
这可以做到吗?
解决方案
如果它在循环内,你不能:
// the start method turns a variable to true for example in the inner function scope
if (start) {
setTimeout((data) => data, 25000);
}
这行得通吗?希望能帮助到你。
推荐阅读
- android - 我们如何获得由android设备选择的可绘制子文件夹名称
- python - 比较 N 个文件的第一列,如果找到匹配,则打印剩余文件的第一列和第二列
- c++ - 转换构造函数和运算符都存在并且涉及显式性时的行为
- flutter - 如何在与firebase一起使用电话号码进行身份验证?
- ios - 当我调用 URLSessionTask 的 task.cancel() 时会发生什么?
- android - 创建 Firebase 列表适配器时获取“类 FirebaseListAdapter 中的构造函数 FirebaseListAdapter 不能应用于给定类型”
- python - 如何使用 Foursquare API 收集有关场地评分的数据
- c# - Xamarin.Forms:如何在 C# 代码中使用自定义字体
- ggplot2 - x 轴组之间的间距大于组间距 geom_col
- python - ImportError:没有命名的模块