如何在JavaScript的睡眠? [英] how to make a sleep in javascript?

查看:88
本文介绍了如何在JavaScript的睡眠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道我怎样才能使一个睡在JavaScript之前下一行被读出系统?

Does anyone know how can I make a sleep in javascript before next line been read by the system?

例如:

	1 var chkResult = Validation();
	2 //sleep here for for 10 sec before the next line been read 	
	3	
	4 document.getElementById('abc').innerHTML = chkResult;

在这个例子中,我怎样才能使JavaScript的睡眠/等待10秒2号线继续之前阅读第4行?我试过的setTimeout('',10000);但它似乎不是为我工作仍然...

For this example, how can I make the javascript sleep/wait in line 2 for 10 sec before it continues to read line 4? I had tried setTimeout('', 10000); but it's seems not working for me still...

推荐答案

我有帽子给予了正确的暗示。使用 的setTimeout 方法执行您的第四行code 10秒后:

I Have the Hat has given the right hint. Use the setTimeout method to execute your forth line code after 10 seconds:

var chkResult = Validation();
var timeout = window.setTimeout(function() {
    document.getElementById('abc').innerHTML = chkResult;
}, 10000);

如果你想明确超时一个变量存放超时ID都可以得心应手。

Storing the timeout ID in a variable can be handy if you want to clear a timeout.

这篇关于如何在JavaScript的睡眠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆