0

嗨,我无法按顺序执行 javascript。事件是如何触发的。即使语句是按顺序放置的,执行也很疯狂。

我需要从 6..to..0 的倒计时数字,然后在 Div 标签中显示随机字符串

JSfiddle 链接

 function getRandomArbitrary(min, max) {     //function to pick question randomly based on math calculations
    var temp = Math.floor(Math.random() * (max - min) + min)

  return (temp > max) ? getRandomArbitrary(min,max): temp;
};

function questions(){       //returns the Question pattern

    var htmlQ = new Array("Canon","Mysql","Cisco","FaceBook","Vimeo","Yahoo","Sony","Ebay","Google","HP","Microsoft","Dell","EAGames","Digg","Adobe");
    var DispatchPattern;
switch(getRandomArbitrary(1,10))
    {
        case 1:
            {
           DispatchPattern = htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)];   
break;
            }
        case 2:
            {

           DispatchPattern = htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)];  
break;
            }
        case 3:
            {
           DispatchPattern = htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)];  
break;
            }
        case 4:
            {
           DispatchPattern = htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)];        
break;
            }
        case 5:
            {
           DispatchPattern = htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)];   
break;
            }
        case 6:
            {
           DispatchPattern = htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)];  
break;
            }
        case 7:
            {
           DispatchPattern = htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)];  
 break;
            }

        case 8:
            {
           DispatchPattern = htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)];
break;
            }
        case 9:
            {
           DispatchPattern = htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)];
break;
            }
        case 10:
            {
           DispatchPattern = htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)]+' - '+htmlQ[getRandomArbitrary(0, 15)];    
          break;
            }
        default: 
            {
            alert("Some Error occured");
            DispatchPattern = 'Biscuit!!!';
            break;
            }
}
    //alert("text: "+DispatchPattern);
return DispatchPattern;
}


function counter($el, n) {
                (function loop() {
                     $el.html(n);
                     if (n--)   {
                                setTimeout(loop, 1000);
                                          }
                     })();
                }


//Sequential execution (i need the count down first then the strings to be displayed in div tag)
$("div").html(counter($("div"), 6)).fadeIn('slow'); $("div").html(questions()).fadeIn('slow');
4

1 回答 1

2

它遵循异步执行模式,因为您使用的是setTimeout.

这里的解决方案是使用回调,一旦倒计时完成就会调用它,并使用回调来显示文本。

function counter($el, n, callback) {
    (function loop() {
        $el.html(n);
        if (n--)   {
            setTimeout(loop, 1000);
        } else {
            callback();
        }
    })();
}


//Sequential execution (i need the count down first then the strings)
$("div").html(counter($("div"), 6, function(){
    $("div").html(questions()).fadeIn('slow');
})).fadeIn('slow');

演示:小提琴

于 2013-05-02T06:49:06.277 回答