javascript - JS如何一举删除/停用/销毁所有方法、类、setTimouts和setIntervals?
问题描述
我有一个创建动画的类。它包括对其他类的调用、其他类的方法、setTimeouts 等。我的任务是制作此动画的退出按钮。通过单击此按钮,所有这些类、方法、循环都应该停止工作。做这个的最好方式是什么?
// build html for animation
class BuildPhone{
constructor(idElement) {
this.create(idElement)
}
create(idElement) {
this.createWrap(idElement)
this.createDisplay()
this.createElements()
}
createWrap(idElement){
document.querySelector('idElement')
// set wrapper for phone
}
createDisplay(){
// set display iphone
}
createElements(){
// set time in display, wi-fi, battery, btn's Home,Back,Apps
}
}
// run chating animation
class Chat{
constructor(messages){
this.messages = messages
this.startChating()
}
async startChating(){
const timeout = (ms) => new Promise(resolve => setTimeout(resolve, ms));
var timerFunc = function(i) {
return async function() {
await timeout(3000); // 3 sek delay before send new message
this.sendMessage(this.messages[i])
this.scrollToBottom()
if (i < this.messages.length) {
setTimeout(timerFunc(++i), 0);
}
}
}
setTimeout(timerFunc(0), 500); // start loop
}
sendMessage(message){
// add message text to list messages in chat
}
scrollToBottom(){
// this code for scrolling to bottom in chat after send message
}
}
class PhoneAnimation{
constructor(args) {
create(args)
}
async create(args) {
// build html for animation
await new BuildPhone(args.idElement)
// run chating animation
await new Chat(args.messages)
}
}
// ------ init -------
args = {
idElement: '#targetElement1',
messages: [
{
from: 'app',
text: 'hello)'
},
{
from: 'user',
text: 'hi'
},
{
from: 'app',
text: 'how are you?'
},
]
}
// init animation
new PhoneAnimation(args)
//HOW TO KILL ALL ANIMATION IF IT RUNED?
结果 - 需要通过某种机制停止类、异步方法和函数、setTimouts 和 setIntervals
解决方案
该setTimeout
函数返回一个可用于停止计时器的处理程序。
var timer;
function tick() {
// do stuff
timer = setTimeout(tick, 1000);
}
function stop () {
if (timer) {
clearTimeout(timer);
timer = undefined;
}
}
编辑:下一部分只是一个粗略的例子(我没有让重新启动成为可能。扩展它很容易。)
var timerHub = {
"timers":{}
,"setTimeout": function (groupName, fn, time) {
groupName = groupName || "@";
var timer = setTimeout(fn ,time);
if (! timers.hasOwnProperty(groupName)) {
timers[groupName] = {"isStopped":false,"list":[]};
}
timers[groupName].list.push(timer);
}
,"isStopped": function (groupName) {
groupName = groupName || "@";
if (! timers.hasOwnProperty(groupName)) {
return true;
}
return timers[groupName].isStopped;
}
,"stop": function (groupName) {
groupName = groupName || "@";
if (! timers.hasOwnProperty(groupName)) {
return;
}
timers[groupName].isStopped = true;
timers[groupName].list.map((t) => {
if (!t) return t;
clearTimeout(t);
return undefined;
});
}
};
推荐阅读
- python - 从 scipy 子模块导入的正确方法
- asp.net-core - ASP.NET Core 2.2 POST 操作参数为空
- visual-studio - 自定义 Visual Studio 离线安装
- c# - JObject 会导致高使用环境中的内存泄漏吗?
- python-3.x - 如何使用输出绘制频率图?
- angular - 使用 http 调用解析可观察的属性
- web-scraping - 如果 class 或 id 不存在,如何在 beautifulsoup 中找到 div
- swift - CoreBluetooth peripheralManager 广告在后台停止
- python - 在字符串中显示随机选择的单词 - Python
- html - bootstrap 4和laravel中的垂直居中问题