gpt4 book ai didi

javascript - 在Javascript中更新setInterval函数的毫秒参数

转载 作者:行者123 更新时间:2023-11-30 16:03:31 25 4
gpt4 key购买 nike

我有一个显示引号的 slider 。有些引述比其他的更长,我想为用户提供足够的时间来阅读它们。计时器变量在 changeQuote 函数中得到更新,但它没有更新 setInterval 函数的参数。如何更新 setInterval 的毫秒参数?谢谢

var quotes=[{quote:'lorem ipsum', name:'test'},{quote:'lorem ipsum ipsum 2', name:'test2'}];

var x=0;
var l = quotes.length;
var ql=0;
var timer=0;

function quoteInit(){
if(x==l) x=0;
ql = quotes[x]['quote'].length;
$('.quote').find('h4').html(quotes[x]['quote']);
$('.quote').find('p').html(quotes[x]['name']);
timer=(ql*110);
x++;
changeQuote(timer);
}

function changeQuote(timer){
console.log(timer);
setInterval(quoteInit,timer);
}

quoteInit();

最佳答案

添加到我的评论中,使用类似这样的东西来停止并再次启动你的计时器:

function start() {
theTimer = setInterval(quoteInit,timer);
};

function stop() {
clearInterval(theTimer);
};

当你想用不同的时间间隔重置计时器时,只需调用 stop()start()(当然是在更改 timer 之后) .这将停止计时器(使用旧的 timer 值)并使用新值重新启动它。

关于javascript - 在Javascript中更新setInterval函数的毫秒参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37356597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com