【发布时间】:2022-08-24 06:05:09
【问题描述】:
此代码旋转网站图标:
function removeAllIcons() {
var allIcons = document.querySelectorAll(\"link[rel=\'icon\']\");
for (let i = 0; i < allIcons.length; i++) {
allIcons[i].remove();
}
}
removeAllIcons();
var favicon_images = [
\'/n-static/img/animated-favicon/tmp-0.gif\',
\'/n-static/img/animated-favicon/tmp-1.gif\',
\'/n-static/img/animated-favicon/tmp-2.gif\',
\'/n-static/img/animated-favicon/tmp-3.gif\',
\'/n-static/img/animated-favicon/tmp-4.gif\',
\'/n-static/img/animated-favicon/tmp-5.gif\',
\'/n-static/img/animated-favicon/tmp-6.gif\',
\'/n-static/img/animated-favicon/tmp-7.gif\',
];
var image_counter = 0; // To keep track of the current image
setInterval(function() {
// Remove if favicon exist.
if(document.querySelector(\"link[rel=\'icon\']\") !== null) {
document.querySelector(\"link[rel=\'icon\']\").remove();
}
// Add new favicon image
document.querySelector(\"head\").insertAdjacentHTML(\'beforeend\', \'<link rel=\"icon\" href=\"\' + favicon_images[image_counter] + \'\" type=\"image/gif\">\');
// If last image then goto first image
// Else go to next image
if(image_counter == favicon_images.length -1)
image_counter = 0;
else
image_counter++;
}, 200);
它的问题是,当浏览器中的一个选项卡被激活时,旋转比激活另一个选项卡时更快。换句话说,如果我们的网站是在浏览器的当前标签页中打开的,那么旋转是相当流畅的。当另一个选项卡被激活时,旋转速度要慢得多。
我已经检查了 Chrome 和 Firefox。这可能是什么原因?
标签: javascript