一个简单的堆栈,逻辑很清晰
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | function Stack(type) { function QueueConst() {} QueueConst.execute_ing=[], QueueConst.execute_no=[], QueueConst.state=1, QueueConst.type = type?type: false QueueConst.prototype.stop=function () { //暂停 QueueConst.state=2 } QueueConst.prototype.reset=function () { //恢复 QueueConst.state=3 QueueConst.prototype.execute(); } QueueConst.prototype.execute=function () { //执行队列 if (QueueConst.state==2) return ; var currentItem = null if (QueueConst.execute_ing.length>0){ currentItem = QueueConst.execute_ing.shift() if (QueueConst.type){ currentItem(QueueConst.prototype.reset) QueueConst.prototype.stop() } else { currentItem() QueueConst.prototype.execute() } //执行当前 } else { if (QueueConst.execute_no.length<1) { //完成队列里面的任务; QueueConst.state = 1 return }; QueueConst.execute_ing = QueueConst.execute_no.reverse() QueueConst.execute_no=[] QueueConst.prototype.execute() } } QueueConst.prototype.add=function (item) { //添加任务 QueueConst.execute_no.push(item) if (QueueConst.state==1) QueueConst.state=3 QueueConst.prototype.execute(); } return new QueueConst() } var que = Stack( true ); que.add(function (next) { var index = 1; var loop = setInterval(function () { console.log(index++) },1000) setTimeout(function () { next();console.log( 'one' ) clearInterval(loop) },5000) }) que.add(function (next) { var index = 1; var loop = setInterval(function () { console.log(index++) },1000) setTimeout(function () { next();console.log( 'two' ) clearInterval(loop) },3000) }) que.add(function (next) { var index = 1; var loop = setInterval(function () { console.log(index++) },1000) setTimeout(function () { next();console.log( 'three' ) clearInterval(loop) },3000) }) |
http://www.cnblogs.com/jiebba/p/6575214.html
http://www.cnblogs.com/jiebba 我的博客,来看吧!
如果有错误,请留言修改下 哦!
感觉可以就推荐下吧!!
感觉可以就推荐下吧!!
感觉可以就推荐下吧!!
感觉可以就推荐下吧!!
感觉可以就推荐下吧!!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
· 用 C# 插值字符串处理器写一个 sscanf