Skip to content

Instantly share code, notes, and snippets.

@abap34
Last active July 19, 2020 11:08
Show Gist options
  • Save abap34/3758b5e798ca8fa2572a9b46bf1e9b02 to your computer and use it in GitHub Desktop.
Save abap34/3758b5e798ca8fa2572a9b46bf1e9b02 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<div id="hoge"> hoge </div>
<body>
<script>
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 500000000; j++) { }
if (i % 2 == 0){
document.getElementById("hoge").innerText = "2"
} else {
document.getElementById("hoge").innerText = "1"
}
console.log("update")
}
document.getElementById("hoge").innerText = "hoge"
</script>
</body>
</html>
@nyk510
Copy link

nyk510 commented Jul 19, 2020

一定時間ごとに hoge/huga が入れ替わるようなコードです

<!DOCTYPE html>
<html>

<body>
  <div id="huga"> huga </div>
</body>

<script>
  const texts = [
    'hoge', 'huga'
  ]
  const dom = document.getElementById('huga')
  window.setInterval(() => {
    // hoge の時 huga (huga のとき hoge) になるように nextText を取得
    const currentIndex = texts.indexOf(dom.innerText)
    const nextText = texts[(currentIndex + 1) % texts.length]
    // innerText の上書き
    dom.innerText = nextText
  }, 2000) // を2秒ごとに実行
</script>

</html>

@abap34
Copy link
Author

abap34 commented Jul 19, 2020

ありがとうございます!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment