如何重复验证码

时间:2015-05-25 15:58:08

标签: javascript audio captcha repeat

所以,我有这个验证码PHP代码,随机生成一个字符串并用它创建一个图像... 现在我正在尝试建立一个重复这些字母的系统。我使用语音合成器为0-9和a-z的字母和数字创建声音。 但是我的代码过于复杂,当我运行它时会崩溃firefox。

<script type="text/javascript" src="js/playAudio.js"></script>
<script type="text/javascript">
var listenString = "12";
var timeout = false;
var currentStringOrder = 0;
function play()
{
    if(currentStringOrder == 0)
    {
        if(listenString[0] == "1")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/1.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "2")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/2.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "3")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/3.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "4")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/4.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "5")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/5.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "6")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/6.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "7")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/7.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "8")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/8.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "9")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/9.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "A")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/a.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "B")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/b.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "C")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/c.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "D")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/d.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "E")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/e.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "F")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/f.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "G")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/g.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "H")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/h.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "I")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/i.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "J")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/j.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "K")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/k.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "L")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/l.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "M")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/m.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "N")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/n.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "O")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/o.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "P")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/p.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "Q")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/q.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "R")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/r.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "S")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/s.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "T")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/t.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "U")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/u.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "V")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/v.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "W")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/w.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "X")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/x.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "Y")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/y.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
        else if(listenString[0] == "Z")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/z.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 1;
        }
    }
    else if(currentStringOrder == 1)
    {
        if(listenString[1] == "1")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/1.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "2")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/2.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "3")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/3.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "4")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/4.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "5")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/5.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "6")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/6.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "7")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/7.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "8")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/8.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "9")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/9.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "A")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/a.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "B")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/b.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "C")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/c.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "D")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/d.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "E")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/e.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "F")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/f.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "G")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/g.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "H")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/h.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "I")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/i.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "J")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/j.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "K")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/k.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "L")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/l.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "M")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/m.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "N")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/n.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "O")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/o.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "P")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/p.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "Q")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/q.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "R")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/r.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "S")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/s.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "T")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/t.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "U")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/u.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "V")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/v.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "W")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/w.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "X")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/x.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "Y")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/y.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
        else if(listenString[1] == "Z")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/z.mp3");
            setTimeout(play(), 1000);
            currentStringOrder = 2;
        }
    }
    else if(currentStringOrder == 2)
    {
        if(listenString[2] == "1")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/1.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "2")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/2.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "3")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/3.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "4")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/4.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "5")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/5.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "6")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/6.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "7")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/7.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "8")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/8.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "9")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/9.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "A")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/a.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "B")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/b.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "C")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/c.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "D")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/d.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "E")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/e.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "F")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/f.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "G")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/g.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "H")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/h.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "I")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/i.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "J")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/j.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "K")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/k.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "L")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/l.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "M")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/m.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "N")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/n.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "O")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/o.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "P")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/p.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "Q")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/q.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "R")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/r.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "S")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/s.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "T")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/t.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "U")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/u.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "V")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/v.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "W")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/w.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "X")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/x.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "Y")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/y.mp3");
            currentStringOrder = 3;
        }
        else if(listenString[2] == "Z")
        {
            playAudio(audioToPlay = "../scripts/captcha/soundtracks/z.mp3");
            currentStringOrder = 3;
        }
    }
}
</script>

1 个答案:

答案 0 :(得分:1)

可能导致脚本崩溃的原因:

setTimeout(play(), 1000);

此语句立即调用play(),它会立即再次调用play,等等。调用setTimeout的正确方法是:

setTimeout(play, 1000);

这将在1000毫秒内调用play,这是预期的行为。

除此之外,您可以通过使用对象大幅减少代码,并执行以下操作:

var urlList = {
  'A': '../scripts/captcha/soundtracks/a.mp3',
  'B': '../scripts/captcha/soundtracks/b.mp3',
  'C': '../scripts/captcha/soundtracks/c.mp3',
};

if(currentStringOrder == 0) {
  if(listenString[0] in urlList) {
    var url = urlList[listenString[0]];
    playAudio(url);
    setTimeout(play, 1000);
    currentStringOrder = 1;
  }
} else if(currentStringOrder == 1) {
  // ...
}