我可以做更多有javascript一次做两件事吗? [英] Can I do more have javascript do two things at once?
问题描述
我有一个奇怪的需要。我想在javascript中执行以下操作:
I have a strange need. I would like to do the following in javascript:
- 调用函数时我想要
来更改DIV的颜色和
然后1/2秒之后我希望
将其更改回来 - 同时(1)我想
喜欢制作一个Ajax调用。通话
通常需要一秒钟
换句话说,我希望步骤1和步骤2同时启动。
In other words I would like step 1 and step 2 to start at the same time.
我对javascript的了解非常基础。这种事可能吗?如果我使用jQuery,那会更容易吗?
My knowledge of javascript is pretty basic. Is this kind of thing possible? How about if I use jQuery, would that make it easier?
推荐答案
你可以使用jQuery及其功能 queue
,以延迟一个动作。
You can use jQuery and its function queue
, to delay an action.
我在jsFiddle上分叉了wdm的版本,把它改成 queue
样式,而不是 setTimeout
。我个人认为,这是一种更清洁,更好的方式来实现您的目标。
I forked wdm's version on jsFiddle, to change it to queue
style, instead of setTimeout
. In my own belief, this is a much cleaner and better way to achieve what you are looking for.
这是分叉演示
以下是JavaScript代码:
Here is the JavaScript code:
$('#change').click(function(e) {
e.preventDefault();
$('#a')
.css('background-color', 'blue')
.delay(500)
.queue(function(next){
$(this).css('background-color', 'red');
next();
});
$.ajax({}); // do the ajax call here
});
这篇关于我可以做更多有javascript一次做两件事吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!