问题描述
香港专业教育学院搜查了很多的答案的地方这个看似简单的问题,但香港专业教育学院只发现涉及类,事件处理程序和回调(这在我看来是有点大锤的方法),而复杂的答案。我认为,回调可能是有用的,但我不能似乎在最简单的情况下应用这些。见这个例子:
Ive searched a lot of places for the answer to this seemingly simple problem, but Ive only found rather complicated answers involving classes, event handlers and callbacks (which seem to me to be a somewhat sledgehammer approach). I think callbacks may be useful but I cant seem to apply these in the simplest context. See this example:
<html>
<head>
<script type="text/javascript">
function myfunction() {
longfunctionfirst();
shortfunctionsecond();
}
function longfunctionfirst() {
setTimeout('alert("first function finished");',3000);
}
function shortfunctionsecond() {
setTimeout('alert("second function finished");',200);
}
</script>
</head>
<body>
<a href="#" onclick="javascript:myfunction();return false;">Call my function</a>
</body>
</html>
在此,第二功能的第一功能之前完成;什么是最简单的方式(或者是有一个?)来强制第二函数来延迟执行,直到第一功能是完成?
In this, the second function completes before the first function; what is the simplest way (or is there one?) to force the second function to delay execution until the first function is complete?
---编辑---
感谢所有的答复这么快!
Thanks for all the replies so quickly!
所以这是一个垃圾的例子,但感谢David Hedlund的我这个新的例子,它的确是同步的看到(在测试过程中撞毁我的浏览器一起!):
So that was a rubbish example but thanks to David Hedlund I see with this new example that it is indeed synchronous (along with crashing my browser in the test process!):
<html>
<head>
<script type="text/javascript">
function myfunction() {
longfunctionfirst();
shortfunctionsecond();
}
function longfunctionfirst() {
var j = 10000;
for (var i=0; i<j; i++) {
document.body.innerHTML += i;
}
alert("first function finished");
}
function shortfunctionsecond() {
var j = 10;
for (var i=0; i<j; i++) {
document.body.innerHTML += i;
}
alert("second function finished");
}
</script>
</head>
<body>
<a href="#" onclick="javascript:myfunction();return false;">Call my function</a>
</body>
</html>
由于我的实际问题是与jQuery和IE浏览器,我将不得不张贴关于一个单独的问题,如果我不能随时随地喽!
As my ACTUAL issue was with jquery and IE I will have to post a separate question about that if I cant get anywhere myself!
推荐答案
好了,的setTimeout
,每它的定义,不会托起线程。这是可取的,因为如果它这样做,它会冻结它在等待的时候,整个UI。如果你真的需要使用的setTimeout
,那么你就应该使用回调函数:
Well, setTimeout
, per its definition, will not hold up the thread. This is desirable, because if it did, it'd freeze the entire UI for the time it was waiting. if you really need to use setTimeout
, then you should be using callback functions:
function myfunction() {
longfunctionfirst(shortfunctionsecond);
}
function longfunctionfirst(callback) {
setTimeout(function() {
alert('first function finished');
if(typeof callback == 'function')
callback();
}, 3000);
};
function shortfunctionsecond() {
setTimeout('alert("second function finished");', 200);
};
如果你的不的使用的setTimeout
,但只具有执行很长的功能,并使用的setTimeout
来模拟,那么你的功能的会的实际上是同步的,你不会有这个问题的。应当指出,虽然,AJAX请求是异步的,并会,就像的setTimeout
直到它已经完成,而不是托起UI线程。随着AJAX,与的setTimeout
,你将有回调的工作。
If you are not using setTimeout
, but are just having functions that execute for very long, and were using setTimeout
to simulate that, then your functions would actually be synchronous, and you would not have this problem at all. It should be noted, though, that AJAX requests are asynchronous, and will, just as setTimeout
, not hold up the UI thread until it has finished. With AJAX, as with setTimeout
, you'll have to work with callbacks.
这篇关于如何强制连续JavaScript的执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!