Possible Duplicate:
Javascript: setInterval() behaviour with 0 milliseconds
我只是想听听,将时间设置为 0 毫秒的 setInterval 是否存在问题?会不会有性能问题?拥有例如会更好吗? 1000 毫秒?
我有这个脚本:
var countdown_id = setInterval(function() {
var now = (new Date()).getTime()-(2*60*60*1000);
;
$('.product').each(function() {
var elm = $(this).attr('id');
var prod_id = $("#"+elm+" #hidden-"+elm).val();
var expires = $('#date-end-' + prod_id).val()*1000;
var seconds_remaining = Math.round((expires - now)/1000);
var hours = FormatNumberLength(Math.floor(seconds_remaining/(60*60)), 2);
var sec_remaining = seconds_remaining-(hours*60*60);
var minutes = FormatNumberLength(Math.floor(sec_remaining/60), 2);
var sec_remaining2 = sec_remaining-(minutes*60);
var seconds = FormatNumberLength(sec_remaining2, 2);
var timestr = hours + ":" + minutes + ":"+seconds;
if (expires) {
if (seconds_remaining > 0) {
$('#'+elm+' .time_left div').text(timestr);
}
else {
$(this).hide();
}
}
$('#'+elm+' .time_left div').text(timestr);
});
}, 0);
提前致谢!
最佳答案
if it is a problem having a setInterval with time set to 0 milliseconds?
您不能将0设置为间隔,因为浏览器会忽略它。
Will there be any performance issues? And will it be better to have e.g. 1000 milliseconds?
当然,如果可能的话,1000 毫秒比 10 毫秒(不是零)要好。
每 10 毫秒做一件事比每 1000 毫秒做一次同样的事情花费更多。
关于javascript - setInterval性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12554531/