如何在javascript中设置时间延迟 [英] How to set time delay in javascript
本文介绍了如何在javascript中设置时间延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的网站上有一块js来切换图像但是当你第二次点击图像时需要延迟。延迟应该是1000毫秒。所以你会点击img.jpg然后会出现img_onclick.jpg。然后,您将单击img_onclick.jpg图像,然后在再次显示img.jpg之前应该延迟1000毫秒。
I have this a piece of js in my website to switch images but need a delay when you click the image a second time. The delay should be 1000ms. So you would click the img.jpg then the img_onclick.jpg would appear. You would then click the img_onclick.jpg image there should then be a delay of 1000ms before the img.jpg is shown again.
以下是代码:
jQuery(document).ready(function($) {
$(".toggle-container").hide();
$(".trigger").toggle(function () {
$(this).addClass("active");
$(".trigger").find('img').prop('src', 'http://localhost:8888/images/img_onclick.jpg');
}, function () {
$(this).removeClass("active");
$(".trigger").find('img').prop('src', 'http://localhost:8888/images/img.jpg');
});
$(".trigger").click(function () {
$(this).next(".toggle-container").slideToggle();
});
});
推荐答案
使用 setTimeout ()
:
Use setTimeout()
:
var delayInMilliseconds = 1000; //1 second
setTimeout(function() {
//your code to be executed after 1 second
}, delayInMilliseconds);
如果你想在没有的情况下这样做> setTimeout
:请参阅此问题。
If you want to do it without setTimeout
: Refer to this question.
这篇关于如何在javascript中设置时间延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文