簡體   English   中英

在函數內循環

[英]Looping within functions

我想重復一段代碼,中間間隔兩秒鍾,我該怎么做?

$(document).keydown(function(e) {
      kkeys.push( e.keyCode );
      if ( kkeys.toString().indexOf( konami ) >= 0 ) {
        $(document).unbind('keydown',arguments.callee);
            $('.preview').attr('style', 'background: #'+Math.floor(Math.random()*16777215).toString(16) + ';');
      }
    });

重復這一點:

$('.preview').attr('style', 'background: #'+Math.floor(Math.random()*16777215).toString(16) + ';');

你可能想要使用一個結合

setTimeout()

對函數和適當的基本情況進行遞歸調用。

你可以保持一個參數作為經過的總時間,並用它來做你想要的。 你也可以使用

setInterval()

這應該會讓你走上正軌

javascript中的簡單循環,不需要jquery ......

// simple for loop
var array = [1,2,3,4];
for (var i = 0; i < array.length; i++){
    console.log(array[i]);
}

// simple while loop
var i = 0;
while (i < 4){
    console.log(array[i]);
    i++;
}

// simple object iteration
var object = { a:1, b:2, c:3, d:4 };
for (var attribute in object){
    if (object.hasOwnProperty(attribute) === true){
        console.log(object[attribute]);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM