console.log("a");sleep(2000);console.log("b");sleep(3000);console.log("c");这个sleep()要怎么实现才能优雅点,
因为sleep()的个数不定,所以直接用setTimeout貌似不太好

解决方案 »

  1.   

    用setInternal()
    var intId = setInterval(addDot,1000);  
    var intId1 = setInterval(adddot,2000);
    function addDot(){  
        if (dotCounter < 10) {  
            dotCounter++;  
            $('#msg').html(dotCounter);  
        } else {  
            clearInterval(intId);    
        }  
    }
    用setInternal()调用函数,一个句子可以设置一个时间
      

  2.   

    function sleep(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return;
        }
    }
    这是一种性能比较低下的代码!可以参考一下!
      

  3.   

    sleep个数不定,数目少的话,用switch,个数多的话找规律,然后用循环和setTimeout不行吗当然,若个数多,也没规律可循,那就没辙了
      

  4.   

    Jscex 框架
    http://www.sndacode.com/projects/jscex
      

  5.   

    这个代码可能可能帮到你:
    //相信你是高手,我就不特别注释了
    <html>
    <head><title>ajax</title>
    <script type="text/javascript" src="JS/jquery-1.4.2.js"></script>
    <script src="JS/jquery-1.4.2-vsdoc.js" type="text/javascript"></script>
    <script type="text/javascript">
    var _Timer =function(){
    var _this =this;
    this.DelayAdd=0;
    this.DelayDo=function(fn,delay){
    this.DelayAdd+=delay;
    setTimeout((function(fun,Da){
    return function(){
    fun();
    };
    })(fn,this.DelayAdd),this.DelayAdd);
    };
    };
    Timer.prototype.TimerId=null;
    Timer.prototype.DelayAdd=0;
    //
    function crossDomain() {
    var t=new _Timer();
    $("#ShowInfo").html("0");
    //========================用法一=================================
    var do1 = function(){$("#ShowInfo").html("1");};
    t.DelayDo(do1,1000);
    //========================用法二=================================
    t.DelayDo(function(){$("#ShowInfo").html("2");},1000);
    //========================用法二=================================
    t.DelayDo(function(){$("#ShowInfo").html("3");},1000);

    }
    </script>
    </head><body> <input type="button" onclick="crossDomain()" value="延时" />
    <div id="ShowInfo">-1</div>
    </body>
    </html>
      

  6.   

    js不支持阻塞吧?除了alert之类的函数。如果只是想让某个函数延迟一段时间再执行的话,可以用回调的方式 var sleep = (function(){
    var _delay = 0;
    return function(fn, delay){
    setTimeout(fn, _delay);
    _delay += delay;
    }
    })();
    sleep(function(){
    console.log('b');
    }, 2000);
    sleep(function(){
    console.log('c');
    }, 2000);
      

  7.   

    改进下 var sleep = (function(){
    var _delay = 0;
    return function(fn, delay){
    _delay += delay;
    setTimeout(function(){
    fn();
    _delay = 0;
    }, _delay);
    }
    })();

    console.log('a');
    sleep(function(){
    console.log('b');
    }, 2000);
    sleep(function(){
    console.log('c');
    }, 2000);
      

  8.   

    用队列的方式好像更好些。 var sleep = (function(){
    var queue = [],
    isFree = true;
    return function(fn, delay){
    var args = arguments,
    self = this;
    if(isFree){
    isFree = false;
    setTimeout(function(){
    fn();
    isFree = true;
    if(queue.length !== 0){
    args.callee.apply(self, queue.shift());
    }
    }, delay);
    }else{
    queue.push(args);
    }
    }
    })();

    console.log('a');
    sleep(function(){
    console.log('b');
    }, 2000);
    sleep(function(){
    console.log('c');
    }, 3000);
      

  9.   

    我顶下:
    axiheyhey
    (牧名)

    继续发!