jquery在x秒后显示和隐藏图像

jquery在x秒后显示和隐藏图像,jquery,Jquery,我想显示和图像2秒钟,然后隐藏它以显示图表。我有一部分是图表出现在2秒钟后,但我不知道如何把图像 $("li").click(function(){ //Toggle List $(this).toggleClass("active"); $(this).next("div").stop('true','true').slideToggle(); //SHOW image for 2 seconds and then HIDE IT var did = $

我想显示和图像2秒钟,然后隐藏它以显示图表。我有一部分是图表出现在2秒钟后,但我不知道如何把图像

$("li").click(function(){
    //Toggle List
    $(this).toggleClass("active");
    $(this).next("div").stop('true','true').slideToggle();
    //SHOW image for 2 seconds and then HIDE IT
    var did = $(this).attr('id');
    var graphic;

    if($(this).next("div").html() == '')
        {
        graphic = '';
            $.ajax({
               url: 'pulse.php?did='+did, success: function(data)
             {
             graphic = data;
             }
        });
    }

    //Here I show the chart after 2 seconds pulling the data from pulse.php...
delay(function(){               
    $("#"+did).next("div").html(graphic);
    }, 2000 );
});


    //Delay function
var delay = (function(){
var timer = 0;
         return function(callback, ms){
         clearTimeout (timer);
         timer = setTimeout(callback, ms);
     };
})();

如果您已经使用了,我想这只是在正确的位置显示和隐藏图像元素的问题:

$("li").click(function(){
    $("#imageElementID").show(); //show image on click
    $(this).toggleClass("active");
    $(this).next("div").stop('true','true').slideToggle();
    //SHOW image for 2 seconds and then HIDE IT
    var did = $(this).attr('id'), graphic;
    if ($(this).next("div").html() === '') {
        graphic = '';
        $.ajax({
            url: 'pulse.php?did='+did, 
            success: function(data) {
                graphic = data;
            }
        });
    }
    delay(function(){               
        $("#imageElementID").hide(); //hide image after some sort of delay function ?
        $("#"+did).next("div").html(graphic);
    }, 2000 );
});​

如果您能很好地格式化代码,并在contextWhere is
delay()
defined中添加一些注释或HTML,这会有所帮助?似乎与
setTimeout
相同?(与jQuery动画
.delay()
函数相比?)能否在代码开头添加
$(“#yourIMG”).show()
,然后将
$(“#yourIMG”).hide()
放在显示图表的同一位置?另外,您似乎是通过Ajax获取图表的,那么为什么不在Ajax成功回调中隐藏图像并显示图表呢?(使用延迟来给Ajax时间来完成并不是一种非常整洁或可靠的方法。)