动画+播放声音在setTimeout函数中不起作用

时间:2016-04-03 02:18:31

标签: javascript jquery

所以评论的警报工作,下面的两个评论也可以在他们所处的位置工作,但是它们在我目前使用它们的功能中不起作用。我猜这是一个范围问题,但我想我会问,因为我不能立即明白它是如何成为范围问题,因为我将它从父母移动到子功能。

$(document).ready(function(){
    // on/off button
    var power = false;
    var start = false;
    var strict = false;
    var simon = [];
    var player = [];
    var green = $(".green-button"); 
    var red = $(".red-button");
    var blue = $(".blue-button");
    var yellow = $(".yellow-button");
    var num;
    var moves = [green, red, blue, yellow];
    var greenSound = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
    var redSound = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
    var blueSound = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
    var yellowSound = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
    var sounds = [greenSound, redSound, blueSound, yellowSound];
    var soundNums = [];
    var count = 0;

    // example of flashing $(".div").fadeOut(200).fadeIn(200)

    $('#on-button').click(function(){
        $(this).toggleClass('on');
        if(power === true)
        {
            power = false;
            if(start === true)
                {
                    start = false;
                    $('#start-button').toggleClass('on');
                }
            if(strict === true)
                {
                    strict = false;
                    $('#strict-button').toggleClass('on');
                }
        }
        else
        {
            power = true;
        }
    });

    $('#start-button').click(function(){
        if(power === true)
        {
                $(this).toggleClass('on');
                if(start === true)
                {
                    start = false;
                }
                else
                {
                    start = true;
                    while(count < 20)
                        {
                            num = Math.floor(4*Math.random());
                            simon.push(moves[num]);
                            soundNums.push(num);
                            for(var i = 0; i < simon.length; i++)
                                {
                                    var timeoutID;
                                    function delayMove() {
                                        timeoutID = window.setTimeout(move, 500);
                                    }
                                    function move() {
                                        $(simon[i]).fadeOut(200).fadeIn(200);
                                        (sounds[soundNums[i]]).play();
                                        //alert('you suck');
                                    }
                                    function clearAlert() {
                                        window.clearTimeout(timeoutID);
                                    }
                                    delayMove();
                                }
                            count++;
                            $("p").text(count); 
                        }

                }
        }
    });

    $('#strict-button').click(function(){
        if(power === true)
        {
                $(this).toggleClass('on');
                if(strict === true)
                {
                    strict = false;
                }
                else
                {
                    strict = true;
                }
        }
    }); 


});

0 个答案:

没有答案