[英]Random Quote generator - How can i set the setInterval after every page load or page refresh?
I have created a random quote generator which runs the quote itself in every 12 sec. 我创建了一个随机报价生成器,它每12秒运行一次报价。 However i want it to change in 2 second after every page load or page refresh .
但是我希望它在每次页面加载或页面刷新后2秒内更改。
function changeQuote()
{
getQuotes();
setInterval(getQuotes, 12000);
}
You want it to be called 2 seconds after page load - then every 12 seconds after that? 您是否希望在页面加载后2秒调用它-然后每12秒调用一次?
Have you tried something like this? 你尝试过这样的事情吗?
function getQuotes() { // This is the function that gets your quotes console.log("Fetched quotes", new Date()); } function firstRun() { getQuotes(); setInterval(getQuotes, 12000); } setTimeout(firstRun, 2000); console.log("Page loaded", new Date());
This will (2 seconds after page load) call firstRun
which gets the quotes - but also sets up our interval for the next runs. 这将(在页面加载后2秒钟)调用
firstRun
来获取报价-并为下一次运行设置间隔。 You can see it in action by pressing the "Run code snippet" button. 您可以通过按“运行代码段”按钮来查看运行中的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.