Skip to content

Commit 06cd579

Browse files
committed
refactor: cache tags
1 parent 8d5dc7e commit 06cd579

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

013-random choice picker/script.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ const createTags = (input) => {
1515
});
1616
};
1717

18-
const pickRandomTag = () => {
19-
const tags = document.querySelectorAll(".tag");
18+
const pickRandomTag = (tags) => {
2019
return tags[Math.floor(Math.random() * tags.length)];
2120
};
2221

@@ -25,9 +24,10 @@ const highlightTag = (tag) => tag.classList.add("highlight");
2524
const unHighlightTag = (tag) => tag.classList.remove("highlight");
2625

2726
const randomSelect = () => {
27+
const tags = document.querySelectorAll(".tag");
2828
const times = 30;
2929
const interval = setInterval(() => {
30-
const randomTag = pickRandomTag();
30+
const randomTag = pickRandomTag(tags);
3131
highlightTag(randomTag);
3232
setTimeout(() => {
3333
unHighlightTag(randomTag);
@@ -37,7 +37,7 @@ const randomSelect = () => {
3737
setTimeout(() => {
3838
clearInterval(interval);
3939
setTimeout(() => {
40-
const randomTag = pickRandomTag();
40+
const randomTag = pickRandomTag(tags);
4141
highlightTag(randomTag);
4242
}, 100);
4343
}, times * 100);

0 commit comments

Comments
 (0)