一个简单的堆栈,逻辑很清晰

简介: function Stack(type) { function QueueConst() {} QueueConst.execute_ing=[], QueueConst.
  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    我的博客,来看吧!

如果有错误,请留言修改下 哦!

相关文章
|
8月前
|
传感器 安全 物联网
0x00 . BlueNRG-1堆栈架构编程 记录总结
0x00 . BlueNRG-1堆栈架构编程 记录总结
|
8月前
清水混毒【逻辑题】
清水混毒【逻辑题】
50 0
|
9月前
项目实战典型案例14——代码结构混乱 逻辑边界不清晰 页面美观设计不足
项目实战典型案例14——代码结构混乱 逻辑边界不清晰 页面美观设计不足
69 0
|
9月前
|
数据采集 安全 程序员
逻辑是个好东西
这些逻辑关系、推导过程与程序中的逻辑结构息息相关。如果你对此不能保持思路清晰,写出的代码很可能与预期有出入,或是在一些特殊情况下存在漏洞。
|
10月前
|
数据可视化
【逻辑思维训练 二】系统思维训练
【逻辑思维训练 二】系统思维训练
84 0
|
11月前
|
安全 Java API
了解程序运行逻辑的必要性及应用和硬件的关系
了解程序运行逻辑的必要性及应用和硬件的关系
60 0
逻辑门电路
随着新技术的发展,集成数字电路类型层出不穷,大量使用大规模功能模块已成为现实。数字电路在众多领域已取代模拟电路,可以肯定,这一趋势将会继续发展下去。 一、逻辑门电路是什么? 逻辑门电路可以分为基本逻辑门和复合逻辑门。具体如何,让我们接下来去了解什么是基本逻辑门,什么是复合逻辑门?
170 0
逻辑门电路
|
Go 索引
SolrCore getSearcher逻辑回顾
假期重新把之前在新浪博客里面的文字梳理了下,搬到这里。
131 0
|
程序员
逻辑为什么重要
易中天先生曾将这种中国逻辑归为三点:问态度,不问事实;问动机,不问是非;问亲疏,不问道理。本文的归纳不如他缜密,却更具体,所指更为鲜明。
1077 0