简体   繁体   English

jQuery使用范围输入更改setInterval的时间

[英]jQuery change time of setInterval using range input

I am trying to access the time value of setInterval, however I am new in jQuery and JavaScript and I am not sure how can I achieve this. 我正在尝试访问setInterval的时间值,但是我是jQuery和JavaScript的新手,我不确定如何实现此目的。 Here is what I have for now: 这是我现在拥有的:

<form name="form4" oninput="amount.value=rangeInput.value">
    <input type="range" id="rangeInput" value="1000" name="rangeInput" min="1000" max="10000" step="500"/>
    <output name="amount" for="rangeInput">1000</output>
</form>

And the code would be: 代码将是:

$(function randomName() {
    setInterval(function(){ 
        //alert("some text");
    }, 10000);
});

Here is a fiddle of what I have for now https://jsfiddle.net/mLk3wb50/ 这是我现在所拥有的小提琴https://jsfiddle.net/mLk3wb50/

Is there a way to change the value of time, link it to the range input? 有没有办法更改时间值,将其链接到范围输入?

Basically, I would like to execute the alert or whatever action at intervals set by the range input. 基本上,我想按范围输入设置的间隔执行警报或任何操作。 Is this possible? 这可能吗? Thanks! 谢谢!

This seems to be the way. 这似乎是这样。 You will need to clear the interval before reinitializing it. 您将需要清除间隔,然后重新初始化。

function someFunc(){ alert("hi"); }
var interval;

$(function randomName() {
    interval = setInterval(someFunc, 10000);;    
});

$("#rangeInput").on("change", function(){
    clearInterval(interval);
    interval = setInterval(someFunc, $(this).val());
});

FIDDLE 小提琴

Here's a way to change the interval: 这是一种更改时间间隔的方法:

 function myFunc(){ alert('hi'); }

 var myInterval = setInterval(myFunc, 1000);

 function changeInterval(){
    clearInterval(myInterval);
    myInterval = setInterval(myFunc, 2000);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM