2014-03-05 36 views
0

jQuery函數我試圖創建功能,以增加數目的div每秒在使用setInterval

$(document).ready(function() {  
$('.number').setInterval(increaseNumber(), 1); 

function increaseNumber() { 
    var num = $(this).html() + 1; 
    $(this).html(num); 
} 

});

http://jsfiddle.net/UY6Q7/3/

這有什麼錯我的代碼,我該如何解決?

回答

2

你可以使用這樣

var num=1; 
setInterval(function(){ 
num++; 
$(".number").html(num); 
},1000); 

DEMO

2

刪除(),傳遞函數但不返回返回的結果。

// and the unit is millisecond, if you mean 1 seconds, then it should be 1000 
// and setInterval is method form window 
window.setInterval(increaseNumber, 1000); 

而且thisincreaseNumberwindow對象,這也是錯誤的。

爲了使您的代碼的工作,你可以檢查以下:

$(document).ready(function() {  
    window.setInterval(increaseNumber, 1000); 

    function increaseNumber() { 
     var num = $('.number'); 
     num.html(+num.html() + 1); 
    } 
}); 

And the working demo.

+0

它不工作太http://jsfiddle.net/UY6Q7/6/ –

+0

@ truslivii.lev檢查我發佈的演示。 – xdazz