जवाबों:
अद्यतन: jQuery 1.4 के रूप में आप .delay( n )विधि का उपयोग कर सकते हैं । http://api.jquery.com/delay/
$('.notice').fadeIn().delay(2000).fadeOut('slow');
नोट : $.show()और $.hide()डिफ़ॉल्ट रूप से पंक्तिबद्ध नहीं हैं, इसलिए यदि आप $.delay()उनके साथ उपयोग करना चाहते हैं, तो आपको उन्हें इस तरह कॉन्फ़िगर करने की आवश्यकता है:
$('.notice')
.show({duration: 0, queue: true})
.delay(2000)
.hide({duration: 0, queue: true});
आप संभवतः क्यू सिंटैक्स का उपयोग कर सकते हैं, यह काम कर सकता है:
jQuery(function($){
var e = $('.notice');
e.fadeIn();
e.queue(function(){
setTimeout(function(){
e.dequeue();
}, 2000 );
});
e.fadeOut('fast');
});
या आप वास्तव में सरल हो सकते हैं और इसे करने के लिए एक jQuery फ़ंक्शन बना सकते हैं।
(function($){
jQuery.fn.idle = function(time)
{
var o = $(this);
o.queue(function()
{
setTimeout(function()
{
o.dequeue();
}, time);
});
};
})(jQuery);
जो (सिद्धांत रूप में, यहाँ पर काम कर रहा है) आपको इसकी अनुमति देगा:
$('.notice').fadeIn().idle(2000).fadeOut('slow');
@Strager द्वारा शानदार हैक। इसे इस तरह jQuery में लागू करें:
jQuery.fn.wait = function (MiliSeconds) {
$(this).animate({ opacity: '+=0' }, MiliSeconds);
return this;
}
और फिर इसका उपयोग करें:
$('.notice').fadeIn().wait(2000).fadeOut('slow');
बेन Alman ने jQuery के लिए एक मिठाई प्लगइन लिखा जिसे doTimeout कहा जाता है। इसमें बहुत सारी अच्छी विशेषताएं हैं!
इसे यहाँ देखें: jQuery doTimeout: setTimeout की तरह, लेकिन बेहतर ।
इसे इस तरह उपयोग करने में सक्षम होने के लिए, आपको वापस लौटने की आवश्यकता है this। वापसी के बिना, fadeOut ('धीमा'), उस ऑपरेशन को करने के लिए एक ऑब्जेक्ट नहीं मिलेगा।
अर्थात:
$.fn.idle = function(time)
{
var o = $(this);
o.queue(function()
{
setTimeout(function()
{
o.dequeue();
}, time);
});
return this; //****
}
फिर ऐसा करें:
$('.notice').fadeIn().idle(2000).fadeOut('slow');
यह jQuery की केवल कुछ पंक्तियों के साथ किया जा सकता है:
$(function(){
// make sure img is hidden - fade in
$('img').hide().fadeIn(2000);
// after 5 second timeout - fade out
setTimeout(function(){$('img').fadeOut(2000);}, 5000);
});
एक काम करने के उदाहरण के लिए नीचे दिए गए फ़िदे देखें ...