読み込みが終了しない場合は、しばらく待つか、リロードを行なってください。
If loading does not finish, wait for a while or reload.
エンジニア向けの情報を発信するブログです。
どなたでも発信できます。
お好きに利用していただれば幸いです。

20230826追記
githubに公開しました。READMEからデモを試すことができます。
パターン2をデプロイしています。
<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8" />
<body>
<script>
let sound = new Audio("./jump01.mp3");
sound.load();
sound.play();
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8">
<body>
<button id="button">音出る</button>
<script>
(document.getElementById('button')).addEventListener('click', function(e) {
e.preventDefault();
let sound = new Audio("./jump01.mp3");
sound.load();
sound.play();
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8" />
<body>
<button id="button">ある要素を見つけたら音出る</button>
<script>
// 👇 ボタン押したら
(document.getElementById('button')).addEventListener('click', function(e) {
e.preventDefault();
// 👇 適当な要素をbody内に埋め込む処理のみ実行
const body = document.querySelector('body');
const div = document.createElement('div');
div.innerText = 'hello';
div.id = 'hello';
body.appendChild(div);
});
let is_played = false;
// 👇 htmlを監視して、ボタンで追加された要素があったら音を鳴らす処理
let interval_id = setInterval(() => {
if (!is_played) {
let hello = document.getElementById('hello');
if (hello) {
let sound = new Audio("./jump01.mp3");
sound.load();
sound.play();
is_played = true;
}
} else {
clearInterval(interval_id);
}
}, 1000);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8" />
<body>
<button id="button">ある要素を見つけたら音出る</button>
<script>
// 👇 soundを定義
let sound = null;
(document.getElementById('button')).addEventListener('click', function(e) {
e.preventDefault();
// 👇 ボタンを押した時にloadしちゃう
sound = new Audio("./jump01.mp3");
sound.load();
const body = document.querySelector('body');
const div = document.createElement('div');
div.innerText = 'hello';
div.id = 'hello';
body.appendChild(div);
});
let is_played = false;
let interval_id = setInterval(() => {
if (!is_played) {
let hello = document.getElementById('hello');
if (hello && sound !== null) {
// 👇 音出す
sound.play();
is_played = true;
}
} else {
clearInterval(interval_id);
}
}, 1000);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8" />
<body>
<button id="button">ある要素を見つけたら音出る</button>
<script>
// 👇 soundを定義
let sound = null;
(document.getElementById('button')).addEventListener('click', function(e) {
e.preventDefault();
// 👇 ファイルを読み込んでミュートで再生しちゃう
sound = new Audio("./jump01.mp3");
sound.load();
sound.muted = true;
sound.play();
const body = document.querySelector('body');
const div = document.createElement('div');
div.innerText = 'hello';
div.id = 'hello';
body.appendChild(div);
});
let is_played = false;
let interval_id = setInterval(() => {
if (!is_played) {
let hello = document.getElementById('hello');
if (hello && sound !== null) {
// 👇 再生時間を最初に戻す
sound.currentTime = 0;
// 👇 ミュートを解除して再生
sound.muted = false;
sound.play();
is_played = true;
}
} else {
clearInterval(interval_id);
}
}, 1000);
</script>
</body>
</html>