【问题标题】:Get a new value every n seconds每 n 秒获取一个新值
【发布时间】:2019-04-02 16:15:50
【问题描述】:

我有一个显示报价的网页。我随机选择其中一个引号。目前它每次加载网页时都会这样做,我现在希望它每 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?

【问题讨论】:

  • 为什么JSON.parse() 带有带有数字键的字符串化对象? O.o quotes 应该是一个引号/字符串数组:quotes = ["Don't worry...", "Keep away...", ...]
  • 稍后会发布。现在只是在玩它来学习。

标签: javascript timer counter


【解决方案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

【讨论】:

  • 非常感谢。我唯一遇到的问题是在最初的 5 秒内没有任何内容可以显示。之后它可以按我的意愿工作。有办法改变吗?
  • @shmink 你是绝对正确的,我已经更新了我的回复,以便立即显示一个报价,然后每 5 秒显示一个新的。
【解决方案2】:

@shmink,很高兴你想出了代码来填充一个 div,我添加了一个 sn-p 来在 setInterval 中调用它,它每 5 秒更新一次。确保在您离开后调用 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>

【讨论】:

  • 非常感谢。我唯一遇到的问题是在最初的 5 秒内没有任何内容可以显示。之后它可以按我的意愿工作。有办法改变吗?
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2022-10-20
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2017-05-09
    相关资源
    最近更新 更多