Skip to content

Commit f339ad3

Browse files
author
dhp94d
committed
feat: 자바스크립트 로딩로직 변경
1 parent 95f938a commit f339ad3

File tree

4 files changed

+31
-79
lines changed

4 files changed

+31
-79
lines changed

js/main.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
function showTopBar() {
22
let country = 'France';
33
let vat = 20;
4-
setTimeout(() => {
5-
document.querySelector('section.country-bar').innerHTML = `<p>Orders to <b>${country}</b> are subject to <b>${vat}%</b> VAT</p>`;
6-
}, 1000);
4+
requestAnimationFrame(() => {
5+
document.querySelector(
6+
'section.country-bar'
7+
).innerHTML = `<p>Orders to <b>${country}</b> are subject to <b>${vat}%</b> VAT</p>`;
8+
});
79
}
810

911
showTopBar();

js/main.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/products.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ async function loadProducts() {
22
const response = await fetch('https://fakestoreapi.com/products');
33
const products = await response.json();
44
displayProducts(products);
5+
// Simulate heavy operation. It could be a complex price calculation.
6+
const heavyOperationAsync = () => {
7+
for (let i = 0; i < 10000000; i++) {
8+
const temp = Math.sqrt(i) * Math.sqrt(i);
9+
}
10+
heavyOperationAsync();
11+
};
512
}
613

714
const onIntersection = (entries, observer) => {
@@ -16,9 +23,25 @@ const onIntersection = (entries, observer) => {
1623

1724
const observer = new IntersectionObserver(onIntersection, {
1825
root: null, // 뷰포트
19-
threshold: 0.1, // 10%가 보이면 콜백 실행
26+
threshold: 0.25, // 25%가 보이면 콜백 실행
2027
});
2128

29+
const productsObserver = new IntersectionObserver(
30+
(entries, observer) => {
31+
entries.forEach((entry) => {
32+
if (entry.isIntersecting) {
33+
// #all-products .container가 뷰포트에 들어왔을 때만 실행
34+
loadProducts();
35+
observer.unobserve(entry.target); // 한 번만 실행되도록 관찰 중지
36+
}
37+
});
38+
},
39+
{
40+
root: null, // 뷰포트 사용
41+
threshold: 0.1, // 10%가 보일 때 실행
42+
}
43+
);
44+
2245
function displayProducts(products) {
2346
// Find the container where products will be displayed
2447
const container = document.querySelector('#all-products .container');
@@ -76,14 +99,3 @@ function displayProducts(products) {
7699
container.appendChild(productElement);
77100
});
78101
}
79-
80-
loadProducts();
81-
82-
// Simulate heavy operation. It could be a complex price calculation.
83-
const heavyOperationAsync = () => {
84-
for (let i = 0; i < 10000000; i++) {
85-
const temp = Math.sqrt(i) * Math.sqrt(i);
86-
}
87-
};
88-
89-
requestIdleCallback(heavyOperationAsync);

js/products.min.js

+1-63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)