0

以下 javascript 代码从数组中随机选择值并将其输入到输入框中。我用过jquery。我想结束 setInterval "zaman2",所以在它结束后我可以在输入框中重新输入下一个随机字符串。但是循环并没有结束并且卡住了。我该如何解决这个问题?链接到 jsFiddle:http: //jsfiddle.net/AQbq4/4/

var dersler = [...very long list...];
var zaman = setTimeout(function() {
    var yeniDers = dersler[Math.floor(Math.random()*dersler.length)];
    sayac = 0;
    var zaman2 = setInterval(function() {
            var harf = yeniDers.slice(0,(sayac+1));
            sayac++;
            $('#main-search').attr('placeholder', harf).typeahead({source: dersler});
        if (sayac == yeniDers.length) {
            clearInterval(zaman2);
        }
    },450);
},2000);
4

1 回答 1

0

你不是说

演示

var tId, tId2;
function show() {
    var ran = arr[Math.floor(Math.random()*arr.length)];
    cnt = 0;
    tId = setInterval(function() {
            var char = ran.slice(0,(cnt+1));
            cnt++;
            $( '#main-search' ).attr('placeholder', char);
        if (cnt == ran.length) {
            clearInterval(tId);
            tId2=setTimeout(show,2000);
        }
    },450);
}
show();
于 2013-01-29T07:14:27.517 回答