LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

VanillaJS便利関数ワンライナー

Last updated at Posted at 2024-04-07
utilityまとめ.js
const $$ = document;
const $ = v => v.startsWith('#') ? $$.getElementById(v.slice(1)) : $$.querySelector(v);
const _$ = (e = "div") => $$.createElement(e);
const DOM = str => new DOMParser().parseFromString(str, "text/html").body.firstElementChild;
const attr = (e, att) => Object.entries(att).map(([k, v]) => e[k] = v);
const css = (e, sty) => Object.entries(sty).map(([k, v]) => e.style[k] = v);
const event = (e, f, v = 'click') => e.addEventListener(v, f);
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
const now = new Date().toISOString().split("T")[0].replaceAll("-", "");
タイマー付き遅延関数.js
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
const delay = async function(f, m) {
    for (i = 1; i <= m; ++i) {
        await sleep(6e4);
        console.log(`\u001b[36m${i}分\u001b[0m経過`);
    }
    console.log('=== \u001b[31m実行\u001b[0m ===');
    f();
};
delay(() => {
    // 処理を書く
}, 5); // 5分後に実行
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up