第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

每n秒獲取一個新值

每n秒獲取一個新值

桃花長相依 2021-04-06 13:14:46
我有一個顯示報價的網(wǎng)頁。我隨機選擇其中一個引號。目前,每次加載網(wǎng)頁時都會執(zhí)行此操作,現(xiàn)在,我希望它每5秒選擇一次。我是一個初學者,不確定如何最好地實現(xiàn)此功能或適當?shù)墓δ?。setInterval?,setTimeout?,delay?,wait?var quotes = JSON.parse('{\    "0": "Don\'t worry about what anybody else is going to do. The best way to predict the future is to invent it. -- Alan Kay", \    "1": "Keep away from people who try to belittle your ambitions. Small people always do that, but the really great make you feel that you, too, can become great. -- Mark Twain", \    "2": "No problem should ever have to be solved twice. -- Eric S. Raymond, How to become a hacker", \    "3": "Attitude is no substitute for competence. -- Eric S. Raymond, How to become a hacker", \    "4": "It is said that the real winner is the one who lives in today but able to see tomorrow. -- Juan Meng", \    "5": "Fools ignore complexity. Pragmatists suffer it. Some can avoid it. Geniuses remove it. -- Alan J.Perlis(Epigrams in programming)", \    "6": "A year spent in artificial intelligence is enough to make one believe in God. -- Alan J.Perlis(Epigrams in programming)" \}');function getRandomArbitrary(min, max) {    return Math.random() * (max - min) + min;}var num = Math.floor(getRandomArbitrary(0, Object.keys(quotes).length));document.getElementById('quote').innerHTML = quotes[num];如前所述,我現(xiàn)在希望ID中的值'quote'每5秒更新一次。所以我認為這意味著更新numvar?
查看完整描述

3 回答

?
ABOUTYOU

TA貢獻1812條經(jīng)驗 獲得超5個贊

很好的是您想出了填充div的代碼,我在setInterval中添加了一個代碼段來調(diào)用該代碼,該代碼每5s更新一次。確保移開后調(diào)用clearInterval以停止setInterval(移至其他頁面,關(guān)閉)。


var quotes = JSON.parse('{\

    "0": "Don\'t worry about what anybody else is going to do. The best way to predict the future is to invent it. -- Alan Kay", \

    "1": "Keep away from people who try to belittle your ambitions. Small people always do that, but the really great make you feel that you, too, can become great. -- Mark Twain", \

    "2": "No problem should ever have to be solved twice. -- Eric S. Raymond, How to become a hacker", \

    "3": "Attitude is no substitute for competence. -- Eric S. Raymond, How to become a hacker", \

    "4": "It is said that the real winner is the one who lives in today but able to see tomorrow. -- Juan Meng", \

    "5": "Fools ignore complexity. Pragmatists suffer it. Some can avoid it. Geniuses remove it. -- Alan J.Perlis(Epigrams in programming)", \

    "6": "A year spent in artificial intelligence is enough to make one believe in God. -- Alan J.Perlis(Epigrams in programming)" \

}');


function updateUI() {

var num = Math.floor(getRandomArbitrary(0, Object.keys(quotes).length));


document.getElementById('quote').innerHTML = quotes[num];

}


function getRandomArbitrary(min, max) {

    return Math.random() * (max - min) + min;

}


var interval = setInterval(updateUI, 5000);


//cleanup

//clearInterval(interval);

<div id="quote"></div>


查看完整回答
反對 回復(fù) 2021-04-15
?
郎朗坤

TA貢獻1921條經(jīng)驗 獲得超9個贊

您可以使用setInterval,它以固定的時間延遲重復(fù)調(diào)用一個函數(shù)或執(zhí)行一個代碼段,并且回調(diào)函數(shù)內(nèi)部num每次都會獲取一個新值。


var quotes = JSON.parse('{\

    "0": "Don\'t worry about what anybody else is going to do. The best way to predict the future is to invent it. -- Alan Kay", \

    "1": "Keep away from people who try to belittle your ambitions. Small people always do that, but the really great make you feel that you, too, can become great. -- Mark Twain", \

    "2": "No problem should ever have to be solved twice. -- Eric S. Raymond, How to become a hacker", \

    "3": "Attitude is no substitute for competence. -- Eric S. Raymond, How to become a hacker", \

    "4": "It is said that the real winner is the one who lives in today but able to see tomorrow. -- Juan Meng", \

    "5": "Fools ignore complexity. Pragmatists suffer it. Some can avoid it. Geniuses remove it. -- Alan J.Perlis(Epigrams in programming)", \

    "6": "A year spent in artificial intelligence is enough to make one believe in God. -- Alan J.Perlis(Epigrams in programming)" \

}');


function getRandomArbitrary(min, max) {

  return Math.random() * (max - min) + min;

}

// you need this value only once so no need to get it at every interval

let maxVal =Object.keys(quotes).length);

setInterval(() => {

  var num = Math.floor(getRandomArbitrary(0,maxVal);

  document.getElementById('quote').innerHTML = quotes[num];

}, 5000)

<div id='quote'></div>


查看完整回答
反對 回復(fù) 2021-04-15
  • 3 回答
  • 0 關(guān)注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號