Skip to content

Commit a282fb4

Browse files
优化
1 parent 294678e commit a282fb4

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

p/index.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,31 +134,35 @@ function reduceImg (img) {
134134
}
135135
}
136136

137+
function stopImgInterval (img) {
138+
clearInterval(img.interval);
139+
img.interval = null;
140+
}
141+
137142
function createImage (pid, n) {
138143
if (pid !== curPid) {
139144
return;
140145
}
141-
if (!(pid > 0)) {
142-
return;
143-
}
144146

147+
// 创建img标签
145148
const img = document.createElement("img");
146-
if (n > 1) {
147-
img.src = `https://pixiv.nl/${pid}-${n}.jpg`;
148-
} else {
149-
img.src = `https://pixiv.nl/${pid}.jpg`;
150-
}
151149
img.id = `${pid}-${n}`;
152150
img.title = img.id;
153151
img.alt = img.id;
152+
if (n > 1) img.src = `https://pixiv.nl/${pid}-${n}.jpg`;
153+
else img.src = `https://pixiv.nl/${pid}.jpg`;
154154
img.style.height = "400px";
155155
img.style.cursor = 'pointer';
156156
img.style.display = 'block';
157+
// 绑定图片点击事件
157158
img.onclick = function () {
158159
window.open(img.src);
159160
};
161+
// 绑定图片加载完成事件
160162
img.onload = function () {
161-
clearInterval(img.interval);
163+
img.onerror = null; // 图片加载成功,将onerror事件移除
164+
stopImgInterval(img);
165+
162166
if (pid !== curPid) {
163167
return;
164168
}
@@ -171,16 +175,16 @@ function createImage (pid, n) {
171175

172176
if (auto.value === '开启') {
173177
setTimeout(function () {
174-
if (pid !== curPid) return;
175178
createImage(pid, n + 1);
176-
}, 1000);
179+
}, 500);
177180
} else {
178181
createImage(pid, n + 1);
179182
}
180183
};
184+
// 绑定图片加载失败事件
181185
img.onerror = function (e) {
182186
console.log(`加载图片 '${img.id}' 失败:`, e);
183-
clearInterval(img.interval);
187+
stopImgInterval(img);
184188

185189
if (pid !== curPid) {
186190
return;
@@ -203,7 +207,7 @@ function createImage (pid, n) {
203207

204208
img.interval = setInterval(function () {
205209
if (reduceImg(img)) {
206-
clearInterval(img.interval);
210+
stopImgInterval(img);
207211
}
208212
}, 10);
209213

@@ -234,7 +238,10 @@ function doEnter (pid, needSavePid) {
234238
// 清空图片
235239
imgs.childNodes.forEach(function (img) {
236240
if (img && img.src) {
237-
clearInterval(img.interval);
241+
stopImgInterval(img);
242+
img.onclick = null;
243+
img.onload = null;
244+
img.onerror = null;
238245
img.src = '';
239246
img.style.height = 'auto';
240247
img.style.width = windowWidth + "px";

0 commit comments

Comments
 (0)