在 Javascript 中对事件进行排序
我正在尝试使用 javascript 制作一个简单的隐藏物体游戏。当用户找到并单击图像时,我希望按以下顺序发生 3 件事;声音响起,图像尺寸增大,然后图像消失。我遇到的问题是让 3 个事件按顺序发生,而不是同时发生。现在看来,这三件事都是同时发生的。
我尝试过使用 setTimeout(),虽然这确实会产生延迟,但它仍然同时运行所有函数,即使每个函数都嵌套在 setTimeout 中。
示例:(这一切只是等待 1.5 秒,然后播放声音并使图像不可见):
function FindIt(image, id){
var t = setTimeout('sound()',10);
var b = setTimeout('bigger(' + image + ')',30);
var h = setTimeout('hide(' + image + ')',1500);
}
以下是我当前使用的功能,实际结果是:单击图像,2 秒内没有任何反应,然后播放声音并图像变得不可见。
function FindIt(image, id){
sound();
bigger(image);
hide(image);
}
function sound(){
document.getElementById("sound_element").innerHTML= "<embed src='chime.wav' hidden=true autostart=true loop=false>";
}
function bigger(image){
var img = document.getElementById(image);
img.style.width = 112;
img.style.height = 112;
}
function hide(id){
var ms = 2000;
ms += new Date().getTime();
while (new Date() < ms){} //Create a 2 second delay
var img = document.getElementById(id);
img.style.visibility='hidden';
}
任何指导将不胜感激!
I am trying to make a simple hidden object game using javascript. When the user finds and clicks an image, I want 3 things to happen in the following order; a sound plays, the image size increases, and the image goes invisible. The problem I am running into is getting the 3 events to happen sequentially, not concurrent. Right now, seems that all three events happen all at the same time.
I've tried using setTimeout(), and while that does create a delay, it still runs all functions at the same time, even if each function is nested in setTimeout.
Example: (all this does is waits 1.5 sec then plays the sound and makes the image invisible):
function FindIt(image, id){
var t = setTimeout('sound()',10);
var b = setTimeout('bigger(' + image + ')',30);
var h = setTimeout('hide(' + image + ')',1500);
}
Below are the functions I am currently using and the actual results are: click the image, nothing happens for 2 seconds, then the sound plays and the image goes invisible.
function FindIt(image, id){
sound();
bigger(image);
hide(image);
}
function sound(){
document.getElementById("sound_element").innerHTML= "<embed src='chime.wav' hidden=true autostart=true loop=false>";
}
function bigger(image){
var img = document.getElementById(image);
img.style.width = 112;
img.style.height = 112;
}
function hide(id){
var ms = 2000;
ms += new Date().getTime();
while (new Date() < ms){} //Create a 2 second delay
var img = document.getElementById(id);
img.style.visibility='hidden';
}
Any guidance would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要按顺序触发事件,您需要在第一个项目完成后一段时间执行第二个项目,在第二个项目完成后一段时间执行第三个项目,等等......
只有您的 sound() 函数实际上需要一些时间,所以我建议如下:
仅供参考,jQuery 或 YUI 等库中的动画功能使此类事情变得更加容易。
另外,请不要在 JS 中使用这种构造:
这会因延迟而锁定浏览器,并且对查看者非常不友好。使用
setTimeout
创建延迟。作为参考,使用 jQuery 中的动画库,jQuery 代码处理对对象的单击,然后在 2 秒内将其动画化到更大的尺寸,延迟 1 秒,然后向上滑动消失,如下所示:
jQuery 管理动画队列并处理设置所有计时器并为您完成所有排序和动画。它的编程变得非常非常容易,并且给出了非常好的结果。
您可以在这里查看它的工作原理和使用方法:http://jsfiddle.net/kC4Mz/。
To trigger things sequentially, you need to execute the second item some amount of time after the first one completes, execute the third item some amount of time after the second one completes, etc...
Only your sound() function actually takes some time, so I'd suggest the following:
FYI, the animation capabilities in libraries like jQuery or YUI make this sort of thing a lot easier.
Also, please don't use this kind of construct in your JS:
That locks up the browser for that delay and is very unfriendly to the viewer. Use
setTimeout
to create a delay.For reference, using the animation libraries in jQuery, the jQuery code to handle a click on the object and then animate it over a 2 second period to a larger size, delay for 1 second, then slideup to disappear is as follows:
jQuery manages an animation queue and handles setting all the timers and doing all the sequencing and animation for you. It's a lot, lot easier to program and gives a very nice result.
You can see it work and play with it here: http://jsfiddle.net/kC4Mz/.
为什么不使用“事件”方法。像 onTaskDone();
why don't use "event" approach. like onTaskDone();
Frame.js 库旨在优雅地处理这样的情况:
与使用标准超时相比,Frame.js 提供了许多优势,特别是如果您正在做很多此类事情(对于游戏来说,您可能会这样做)。
https://github.com/bishopZ/Frame.js
The Frame.js library is designed to elegantly handle situations like this:
Frame.js offers many advantages over using standard timeouts, especially if you are doing a lot of this kind of thing, which for a game, you likely are.
https://github.com/bishopZ/Frame.js