This skill helps you diagnose and fix DOM race conditions and optimistic UI in vanilla JavaScript across Live2D and VRM components, improving reliability.
npx playbooks add skill project-n-e-k-o/n.e.k.o --skill vanilla-js-ui-race-conditions
`javascript
const bindEvents = () => {
// 兼容多个 ID 解析
const getEl = (ids) => {
for (let id of ids) {
const el = document.getElementById(id);
if (el) return el;
}
return null;
};
const targetEl = getEl(['live2d-agent-keyboard', 'vrm-agent-keyboard']);
// 如果没找到,自己建立一个 500ms 后的新宏任务来重试,然后 return 本次任务。
if (!targetEl) {
setTimeout(bindEvents, 500);
return;
}
// 找到了,顺次执行并结束,不会留下任何定时器垃圾
targetEl.addEventListener('change', myLogic);
myLogic(); // 手动触发第一次检查
};
setTimeout(bindEvents, 100); // 发动第一下
`
`javascript
const checkState = () => {
const isUiInteractive = domCheckbox && !domCheckbox.disabled;
let isFeatureOn = false;
if (!isUiInteractive) {
// UI 在转圈加载,信任乐观状态或后端缓存
isFeatureOn = optimisticState !== undefined ? optimisticState : backendFlags;
} else {
// UI 处于可用交互态,百分百信任当前 DOM
isFeatureOn = optimisticState !== undefined ? optimisticState : domCheckbox.checked;
}
}
`
Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.