[英]I can't get setInterval() to work in Javascript
i have an algorithm that generates permutations of a given word. 我有一个算法,可以生成给定单词的排列。 I'm trying to use
setInterval()
to generate the next permutation but the function runs only once! 我正在尝试使用
setInterval()
来生成下一个排列,但该函数只运行一次! I can't figure out why. 我无法弄清楚为什么。 I don't get any error messages.
我没有收到任何错误消息。 Here is my code
这是我的代码
var splitted;
var t;
$(document).ready(function() {
$('#SubmitBtn').click(function() {
//change Start to Stop and change button id
$('#SubmitBtn').attr('id','StopBtn').attr('value','Stop');
//and add click event to it
$('#StopBtn').click(function() {
clearInterval(t);
$('#StopBtn').attr('value','Submit');
$('StopBtn').attr('id','SubmitBtn');
});
if ($('#AnagramTxtArea').val().length>0)
$('#AnagramTxtArea').text('');
var inputTxt = $('#anagram').val();
splitted = inputTxt.split("");
splitted.sort(); //first sort the array in order to generate permutations
$('#AnagramTxtArea').append(splitted.join("") + " ");
t= setInterval(GeneratePermutation(),10);
});
});
var AnagramObj = new Anagram();
function GeneratePermutation() {
splitted = AnagramObj.NextPermutation(splitted);
if (splitted!=null)
$('#AnagramTxtArea').append(splitted.join("") + " ");
else
$('#StopBtn').click();
}
and HTML: 和HTML:
<div id="content">
<input id="anagram" type="text" placeholder="Insert your text here" maxlength="80"/> <br />
<input id="SubmitBtn" type="submit" value="submit" />
<br />
<textarea id="AnagramTxtArea" readonly="readonly"></textarea>
</div>
EDIT: Yet, another problem: 编辑:然而,另一个问题:
When calling $('#StopBtn').click() code continues to execute after existing the click event function. 当调用$('#StopBtn')时,click()代码在现有click事件函数后继续执行。 So i'm in an infinite loop.
所以我处在一个无限循环中。
You need to pass the function object itself to setInterval()
, not the result of a call to the function: 您需要将函数对象本身传递给
setInterval()
,而不是调用函数的结果:
t = setInterval(GeneratePermutation,10);
// ^ No parentheses
EDIT: On your second question, what you can do is check whether the interval is running. 编辑:关于第二个问题,你可以做的是检查间隔是否正在运行。 If it is, then cancel it:
如果是,则取消它:
var splitted;
var t;
$(document).ready(function() {
$('#SubmitBtn').click(function() {
if (t !== undefined) { //interval is already running
clearInterval(t);
t = undefined;
$('#SubmitBtn').attr('value','Submit');
} else {
//change Start to Stop
$('#SubmitBtn').attr('value','Stop');
if ($('#AnagramTxtArea').val().length>0)
$('#AnagramTxtArea').text('');
var inputTxt = $('#anagram').val();
splitted = inputTxt.split("");
splitted.sort(); //first sort the array in order to generate permutations
$('#AnagramTxtArea').append(splitted.join("") + " ");
t = setInterval(GeneratePermutation,10);
}
});
});
Here you actually execute GeneratePermutation()
at the time you call setInterval(): 在这里,您实际上在调用setInterval()时执行
GeneratePermutation()
():
t = setInterval(GeneratePermutation(),10);
You have to pass a function to setInterval(). 您必须将函数传递给setInterval()。 Do this instead:
改为:
t = setInterval(GeneratePermutation, 10);
You must pass in the function name or an anonymous function as the first argument for setInterval. 您必须传入函数名称或匿名函数作为setInterval的第一个参数。 See MDN reference https://developer.mozilla.org/en/window.setInterval
请参阅MDN参考https://developer.mozilla.org/en/window.setInterval
t = setInterval(GeneratePermutation,10);
or 要么
t = setInterval(function() { /* code */ }, 10);
proper syntax for setInterval: setInterval的正确语法:
var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
var intervalID = window.setInterval(code, delay);
where 哪里
Edit: This could be a problem, assuming you are trying to fire a click. 编辑:这可能是一个问题,假设您尝试触发点击。
function GeneratePermutation() {
splitted = AnagramObj.NextPermutation(splitted);
if (splitted!=null)
$('#AnagramTxtArea').append(splitted.join("") + " ");
else
//$('#StopBtn').click();
// should be
$('#StopBtn').trigger('click');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.