如何将参数传递给setInterval毫秒值?

时间:2014-09-23 04:12:22

标签: javascript

如何传递输入字段的值以使其成为函数的间隔?

function myTasksInProgressNext() {  
    // the value of this is 1 for example;
    var x = parseInt(document.getElementById('txt').value);
    var y = x * 60000;
    window.open('remindInProgress.php','blank', 'width=200, height=200');
}
var t3 = setInterval(function(){myTasksInProgressNext()}, y);

这是正确的吗?

2 个答案:

答案 0 :(得分:0)

您应该在函数外定义xy变量:

var x = parseInt(document.getElementById('txt').value);
var y = x * 60000;

function myTasksInProgressNext() {  
    window.open('remindInProgress.php','blank', 'width=200, height=200');
}

var t3 = setInterval(function () {
    myTasksInProgressNext();
}, y);

答案 1 :(得分:0)

HTML:

<form id="interval-form">
    <input type="text" name="timeout" value="1000"/>
</form>

<div id="start-btn" onclick="onStartClick()" >START</div>

JS:

function runThis() {
    /* code */
}

var interval;

function onStartClick() {
    var form = document.getElementById('interval-form');

    var timeout = parseInt(form['timeout'].value, 10);

    interval = setInterval(runThis, timeout)
}