每n秒获取一个新值

时间:2019-04-02 16:15:51

标签: javascript timer counter

我有一个显示报价的网页。我随机选择其中一个引号。目前,每次加载网页时都会执行此操作,现在我希望它每5秒选择一次。我是一个初学者,不确定如何最好地实现此功能或适当的功能。 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];

如上所述,我现在希望ID 'quote'中的值每5秒更新一次。因此,我认为这意味着更新num var?

3 个答案:

答案 0 :(得分:1)

您确实可以使用setInterval来完成此任务。尝试像这样添加setInterval:

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;
    }
    
    function updateQuote() {
      var num = Math.floor(getRandomArbitrary(0, Object.keys(quotes).length));
      document.getElementById("quote").innerHTML = quotes[num];
    }
    
    updateQuote();
    setInterval(updateQuote, 5000);
<h3 id="quote"></h3>

setInterval的第二个参数接受毫秒数,因此在您的情况下为5000。阅读更多here

答案 1 :(得分:0)

您可以使用setInterval来重复调用一个函数或执行一段代码,具有固定的时间延迟,并且在回调函数内部每次都会获得一个新的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>

答案 2 :(得分:0)

@shmink,很好的是您想出了填充div的代码,我添加了一个片段以在setInterval中进行调用,该间隔每5s更新一次。确保移开后调用clearInterval以停止setInterval(移至其他页面,关闭)。

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>