JavaScript 流程控制器
已知有流程step1、step2、step3、step4、step5 , 如何控制输出下面过程
例如:
1:step1、step2、step3、step2、step3、step4、step5
2:step1、step2、step4、step5
3:step1、step2、step4、step5、step3、step4、step5
/*
* 流程控制器
* 作者:caoke
* */ class Step{
//初始化
constructor(stepArr,callback){ this.stepArr=stepArr;
this.curIndex=0;
this.isPaused=false;
this.nextMode=null;
this.curStep=this.stepArr[this.curIndex];
this.hasRunTimes={};
this.stepArr.forEach( (step)=> {
this.hasRunTimes[step]=0;
})
this.callback=callback;
}
callback(){
this.go()
}
// 运行当前流程
run(){
this.curStep=this.stepArr[this.curIndex]
if(this.curStep){
this.hasRunTimes[this.curStep]++
this.callback&&this.callback.apply(this,[this.curStep,this.hasRunTimes[this.curStep]])
}
}
// 跳转到某个流程
go(step){
this.clear()
if(step){
this.curIndex=this.stepArr.indexOf(step)
}else{
this.curIndex++
}
this.run()
} // 进入下一个流程
next(){
if(this.nextMode){
this.go(this.nextMode.nextStep)
}else{
this.go()
}
} // 自动进入下一步
waitSecondAndGo(second,step){
if(!this.isPaused){
this.stopTimer()
}
const mode={
startTime:new Date().getTime(),
allSecond:second,
leftSecond:second*1000,
nextStep:step,
timer:null,
} //获得下一步
if(this.nextMode==null||mode.leftSecond<this.nextMode.leftSecond){
this.nextMode=mode;
} if(!this.isPaused){
this.startTimer()
} }
// 暂停
pause(){
if(!this.isPaused){
this.isPaused=true;
this.stopTimer() } }
// 继续
repause(){
if(this.isPaused){
this.isPaused=false;
this.startTimer()
} }
stopTimer(){
if(this.nextMode&&this.nextMode.timer){
const duration=new Date().getTime()-this.nextMode.startTime;
this.nextMode.leftSecond=this.nextMode.leftSecond-duration; clearTimeout(this.nextMode.timer);
this.nextMode.timer=null;
}
}
startTimer(){
if(this.nextMode&&this.nextMode.timer==null){
this.nextMode.startTime=new Date().getTime();
this.nextMode.timer=setTimeout(() => {
this.go(this.nextMode.nextStep)
},this.nextMode.leftSecond)
}
}
// 销毁
clear(){
if(this.nextMode){
if(this.nextMode.timer){
clearTimeout(this.nextMode.timer);
this.nextMode.timer=null
}
this.nextMode=null
}
}
}
export default Step;
JavaScript 流程控制器的更多相关文章
- 第一百零一节,JavaScript流程控制语句
JavaScript流程控制语句 学习要点: 1.语句的定义 2.if 语句 3.switch语句 4.do...while语句 5.while语句 6.for语句 7.for...in语句 8.br ...
- Javascript流程控制
Javascript流程控制 1.条件语句 (1)if(exp)执行一句代码 (2)if(exp){执行代码段;} (3)if(exp){exp为true执行代码段}else{exp为false执行的 ...
- JavaScript流程控制语句脑图
JavaScript流程控制语句脑图 图片是从网上找来的,在这记录一下,以备后面需要的时候查找方便. JavaScript通过规定的语句让有条件的按照一定的方式执行. 分为:循环语句 while do ...
- JavaScript 流程控制-分支
JavaScript 流程控制-分支 1.流程控制 在一个程序执行的过程中,各条代码的执行顺序对程序的结果是有直接影响的,很多时候我们要通过控制代码的执行顺序来实现我们要完成的功能. 简单理解:流程控 ...
- Javascript 流程控制
流程控制 JavaScript通过流程语句来执行程序流,程序流有若干语句组成.在正常情况下,程序中 的语句时按照书写顺序执行的,这种结构称为顺序结构.除了顺序结构之外还有选择结构和循环结构. 1.选择 ...
- JavaScript流程控制及函数
1 流程控制 1.1 条件语句 分支结构 单向分支 if (条件表达式) { code...} 双向分支 if (条件表达式){ } else { } <!DOCTYPE html& ...
- javaScript流程控制与函数
流程控制 1.1 条件语句 分支结构 单向分支 if (条件表达式) { code... } <!DOCTYPE html> <html> <head> <m ...
- javascript 流程控制及函数
回顾 基本语法 在html的使用 <script></script> 注释 ///* */ 指令结束符 ;换行 输出 console.log()document.write() ...
- JavaScript流程控制语句
一.JavaScript分支语句 -alert() 弹出警告对话框 -prompt() 弹出输入框 1.if(){}else{} 栗子: var num=prompt("请输入电话号码 ...
随机推荐
- Use formatter to format your JAVA code
In order to make the codes looks unified and make it easy to understand, it's better to use the same ...
- [Jenkins] Jenkins 执行 Composite 模式的 SoapUI Project
cd %WORKSPACE% cmd /c call "D:\Program Files\SmartBear\ReadyAPI-1.3.1\bin\testrunner.bat" ...
- .net正则查询
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- JavaScript 与JQuery 常用方法比较
http://drupalchina.cn/content/javascript-yu-jquery-chang-yong-fang-fa-bi-jiao 1.加载DOM区别 JavaScript: ...
- zuluCryt cli howto
1.解锁卷的命令. zuluCrypt-cli -o -d /dev/sdc1 -m blabla -e ro -f /home/keyFile zuluCrypt-cli -o -d /dev/sd ...
- linux 命令学习-网络相关配置
网络配置相关 网卡配置文件:etc/sysconfig/network-scripts/ifcfg-eth0 DNS 配置文件 etc/resolv.conf 主机配置文件 etc/sysconfig ...
- ssm集合的配置
web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quo ...
- Codeforces 691E Xor-sequences(矩阵快速幂)
You are given n integers a1, a2, ..., an. A sequence of integers x1, x2, ..., xk is called a & ...
- JavaScript的词法作用域问题
多年以前,当我怀揣着前端工程师的梦想时,曾经认真阅读过<JavaScript高级程序设计(第2版)>.里面有一个问题(P147),让我一直百思不得其解. function createFu ...
- ASP.NET mvc4 Controllder 同步还是异步
从抽象类Controller 的定义可以看出他 同时实现了 IAsyncController, IController public abstract class Controller : Contr ...