簡體   English   中英

js代碼在控制台中運行但不是由實時服務器運行

[英]js code running in console but not by live server

我是 js 的 biginner 編碼器。 我正在嘗試制作一個直到 100 的計數器。給定的代碼在控制台中運行良好,但實時服務器在瀏覽器中運行計數器。 ` JS

var count=1;
setInterval(()=>{
    if(count!=100)
    {
         count++;
         k.innerHTML=count  
    }
},10)

HTML

<html>
<head>
<style>
        body{
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            height: 80vh;
        }
    </style>
</head>
<body>
    <p onclick="hell()">counter</p>
    <div><span id="oe">1</span>
        <i class="fa-solid fa-play"></i>
    </div>
</body>
</html>

倒計時達到 100。它在瀏覽器中完美運行。 在此處輸入圖像描述

嘗試這個:

 let count=0; let timer = 1; let k = null; let para = null; document.addEventListener("DOMContentLoaded", onDomContentLoaded); function onDomContentLoaded(e){ k = document.getElementById("oe"); para = document.getElementById("myParagraph"); para.addEventListener("click",hell); } function countFunction(){ if(count<100){ count++; console.log(count); k.innerHTML=count; } if(count===100){ clearInterval(timer); count = 0; } } function hell(){ if(count===0){ timer = setInterval(countFunction,10); } }
 body{ display: flex; justify-content: center; align-items: center; flex-direction: column; height: 80vh; }
 <p id="myParagraph">counter</p> <div><span id="oe">1</span> <i class="fa-solid fa-play"></i> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM