2014-03-05 39 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