setInterval 性能 [英] setInterval performance
问题描述
可能的重复:
Javascript:0 毫秒的 setInterval() 行为
我只是想听听,如果 setInterval 的时间设置为 0 毫秒有问题吗?会不会有性能问题?并且最好有例如1000 毫秒?
I simply want to hear, if it is a problem having a setInterval with time set to 0 milliseconds? Will there be any performance issues? And will it be better to have e.g. 1000 milliseconds?
我有这个脚本:
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);
提前致谢!
推荐答案
Javascript 不是多线程的,它只是通过延迟执行来模拟它.当计时器达到您的超时间隔时,它将暂停所有其他执行并运行您告诉它的代码.因此,您可能需要注意您的时间间隔有多短.如果您不小心,它可能会淹没您的浏览器.
Javascript is not multithreaded, it just simulates it with delayed execution. When the timer reaches your timeout interval, it will pause all other execution and run the code you told it to. Because of this, you might want to be careful about how short your interval is. It will probably tank your browser if you aren't careful.
这篇关于setInterval 性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!