我撰寫了以下內容,它以 5 秒的間隔成功地重復了腳本。但是,我希望它在沒有 5 秒延遲的情況下啟動。是否可以立即運行腳本一次,然后啟動它?
<body onload="Run()">
<p id = "Text" style =
"text-align:right; font-size: 100%; font-weight: bold; font-style: italic; color: black; font-family:poppins;">
</p>
<script>
var down = document.getElementById('Text');
var arr = [
"\"The end of the liver and the beginning of reading.\"",
"\"The most straight geniuses intent lemur sand,\"",
"\"Passim abandonment to fashion,\"",
];
setInterval(function Run() {
down.innerHTML =
arr[Math.floor(Math.random() * arr.length)];
}, 5000) </script>
uj5u.com熱心網友回復:
抱歉,如果我在這里打斷了你的小貓捉老鼠游戲 ;-)
以下代碼段包含一些使腳本正常作業的更正:
Run()我獨立于setInterval()呼叫定義了函式- 然后我將其稱為
setInterval().
function Run() {
down.innerHTML=arr[Math.floor(Math.random() * arr.length)]; }
const down= document.getElementById('Text');
const arr = ["hello","goodbye","and whatever","I wanted to say"];
setInterval(Run,5000);
<body onload="Run()">
<p id = "Text" style =
"text-align:right; font-size: 100%; font-weight: bold; font-style: italic; color: black; font-family:poppins;">
</p>
在 SO 片段中,該<script>部分將始終在加載 HTML 部分后呼叫。這當然是您必須在自己的檔案結構中確保的內容。實作這一目標的最簡單方法是將<script>部分放在<body>.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/485382.html
