我有一个Google图..通过setinterval函数进行引用...现在,当从下拉列表中选择一个值时,我面临着动态设置setinterval时间的问题。例如,如果下拉值是a或b,则间隔时间将是5秒,如果值是c或d,则间隔时间将是5分钟。...在页面加载时,我将获得由$选择的下拉值_GET方法......
<select onchange=changeintervaltime()>
<option value="a"></option>
<option value="b"></option>
<option value="c"></option>
<option value="d"></option>
</select>
setInterval(setRefreshInterval, 5000);
function setRefreshInterval()
{
//logic goes here
}
function changeintervaltime()
{
//here i want to set the interval time dynamically
}
setRefreshInterval该函数将始终被调用,我想在选择框更改时动态设置间隔时间...。我将如何做到这一点?
答案 0 :(得分:0)
您必须清除间隔并使用新间隔重新调用setInterval
。
let interval = setInterval(setRefreshInterval, 1000);
function setRefreshInterval()
{
console.log('running');
}
function changeintervaltime()
{
clearInterval(interval);
interval = setInterval(setRefreshInterval, 500);
}
<select onchange=changeintervaltime()>
<option value="a"></option>
<option value="b"></option>
<option value="c"></option>
<option value="d"></option>
</select>
答案 1 :(得分:0)
尝试一下:
<select onchange=changeintervaltime()>
<option value="a"></option>
<option value="b"></option>
<option value="c"></option>
<option value="d"></option>
</select>
var interval = setInterval(setRefreshInterval, 5000);
function setRefreshInterval()
{
//logic goes here
}
function changeintervaltime()
{
//here i want to set the interval time dynamically
//suppose you want to set timer for 3000 now so clear the interval and set it again
clearInterval(interval);
interval = setInterval(setRefreshInterval, 3000);
}
答案 2 :(得分:0)
您可以根据下拉值设置setInterval值。
let timer = 1000 ; // Default timer value
let interval = setInterval(setRefreshInterval, timer);
function setRefreshInterval()
{
// logic ...
}
function changeintervaltime(dropDownValue)
{
if(dropDownValue == 'a' || dropDownValue =='b'){
timer = 5000 // 5ms
}else if(dropDownValue == 'c' || dropDownValue == 'd'){
timer = 30000 // 5min
}
clearInterval(interval);
interval = setInterval(setRefreshInterval, timer);
}
<select onchange=changeintervaltime(this.value)>
<option value="a"></option>
<option value="b"></option>
<option value="c"></option>
<option value="d"></option>
</select>