如何在我的 javascript 中延迟以下之间的执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5990725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delay execution in between the following in my javascript
提问by learning
I want to delay execution in betwen the follwoing codes:
我想在以下代码之间延迟执行:
$("#myName").val("Tom");
///delay by 3s
$("#YourName").val("Jerry");
//delay by 3s
$("#hisName").val("Kids");
回答by T.J. Crowder
You can use setTimeout
for that:
你可以使用setTimeout
:
setTimeout(function() {
// Your code here
}, delayInMilliseconds);
E.g.:
例如:
$("#myName").val("Tom");
/// wait 3 seconds
setTimeout(function() {
$("#YourName").val("Jerry");
/// wait 3 seconds
setTimeout(function() {
$("#hisName").val("Kids");
}, 3000);
}, 3000);
setTimeout
schedules a function to be run (once) after an interval. The code calling it continues, and at some point in the future (after roughly the time you specify, though not precisely) the function is called by the browser.
setTimeout
安排一个函数在一个时间间隔后运行(一次)。调用它的代码会继续,并且在将来的某个时间点(大致在您指定的时间之后,尽管不精确),浏览器会调用该函数。
So suppose you had a function called output
that appended text to the page. The output of this:
因此,假设您有一个名为output
将文本附加到页面的函数。这个的输出:
foo();
function foo() {
var counter = 0;
output("A: " + counter);
++counter;
setTimeout(function() {
output("B: " + counter);
++counter;
setTimeout(function() {
output("C: " + counter);
++counter;
}, 1000);
}, 1000);
output("D: " + counter);
++counter;
}
...is (after a couple of seconds):
...是(几秒钟后):
A: 0 D: 1 B: 2 C: 3
Note the second line of that. The rest of foo
's code runs before either of the scheduled functions, and so we see the D
line before the B
line.
注意第二行。的其余foo
代码在任一预定函数之前运行,因此我们看到该D
行之前的B
行。
setTimeout
returns a handle (which is a non-zero number) you could use to cancel the callback before it happens:
setTimeout
返回一个句柄(它是一个非零数字),您可以用来在回调发生之前取消它:
var handle = setTimeout(myFunction, 5000);
// Do this before it runs, and it'll never run
clearTimeout(handle);
There's also the related setInterval
/ clearInterval
which does the same thing, but repeatedly at the interval you specify (until you stop it).
还有相关的setInterval
/clearInterval
做同样的事情,但在你指定的时间间隔重复(直到你停止它)。
回答by Jad
You can use the setTimeout function. I think the syntax is
您可以使用 setTimeout 函数。我认为语法是
window.setTimeout('$("#YourName").val("Jerry")',3000);
回答by Matt
You cannot "delay" in JavaScript without locking the browser; i.e the user cannot move their mouse or click anything (undesirable for 3+ seconds!).
在不锁定浏览器的情况下,您无法在 JavaScript 中“延迟”;即用户不能移动他们的鼠标或点击任何东西(不希望超过 3 秒!)。
Instead, you should look at setting a timeout, which will execute designated code some time in the future...
相反,您应该考虑设置一个超时时间,它将在未来某个时间执行指定的代码......
$("#myName").val("Tom");
setTimeout(function () {
$("#YourName").val("Jerry");
setTimeout(function () {
$("#hisName").val("Kids");
}, 3000);
}, 3000);
You can check out the documentation for setTimeout
here: https://developer.mozilla.org/en/window.setTimeout. The basics of it is that you pass either a function reference, or a string (which should be avoided however), as the first parameter, with the second parameter specifying how many milliseconds the code should be delayed for.
您可以在setTimeout
此处查看文档:https: //developer.mozilla.org/en/window.setTimeout。它的基础是你传递一个函数引用或一个字符串(但是应该避免)作为第一个参数,第二个参数指定代码应该延迟多少毫秒。
回答by nilfalse
If delay is always the same (3s in your example), you may avoid nested code and use setInterval
instead of setTimeout
:
如果延迟始终相同(在您的示例中为 3s),您可以避免嵌套代码并使用setInterval
代替setTimeout
:
var i
, ids = ["myName", "YourName", "hisName"]
, names = ["Tom", "Jerry", "Kids"];
i = setInterval(function () {
if (ids.length > 0) {
$("#" + ids.shift()).val(names.shift());
} else {
clearInterval(i);
}
}, 3000);