Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

javascript - 如何在javascript中设置时间延迟(How to set time delay in javascript)

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.(我在我的网站上有一块js来切换图像但是当你第二次点击图像时需要延迟。)

The delay should be 1000ms.(延迟应该是1000毫秒。) So you would click the img.jpg then the img_onclick.jpg would appear.(所以你会点击img.jpg然后会出现img_onclick.jpg。) You would then click the img_onclick.jpg image there should then be a delay of 1000ms before the img.jpg is shown again.(然后,您将单击img_onclick.jpg图像,然后在再次显示img.jpg之前应该延迟1000毫秒。) Here is the code:(这是代码:) 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(); }); });   ask by Blue Orange translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use setTimeout() :(使用setTimeout() :)

var delayInMilliseconds = 1000; //1 second setTimeout(function() { //your code to be executed after 1 second }, delayInMilliseconds); If you want to do it without setTimeout : Refer to this question .(如果你想在没有setTimeout情况下这样做:请参阅这个问题 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...