问题描述:

Even when there are 5+ divs on screen it keeps going, any way to fix this?

if ($('div').length > 5) {

// Do nothing

} else {

window.setInterval(function(){

$(document).ready(function() {

$('body').append('<div>' + (Math.floor(Math.random() * 9) + 0) + '</div>');

});

}, 1000);

}

网友答案:

It sounds like what you want is to clear the interval timer when 5 are created so you would need to check again inside the interval timer code also

Something like:

var timer= window.setInterval(function(){
    if ($('div').length > 5){
       clearInterval(timer)
    }else{
        $('body').append('<div>' + (Math.floor(Math.random() * 9) + 0) + '</div>');
    }     

  }, 1000);
相关阅读:
Top