2012-07-03 48 views
0

我想顯示圖像2秒鐘並隱藏它以顯示圖表。我有部分圖表在2秒後出現,但我不知道如何放置圖像。jquery在x秒後顯示並隱藏圖像

$("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); 
    }; 
})(); 
+0

,如果你很好地格式化你的代碼,並把它有助於一些註釋或HTML在上下文中 –

+0

「delay()」在哪裏定義?看起來和'setTimeout'一樣? (與jQuery動畫'.delay()'函數相比)你能不能在你的代碼開頭添加'$(「#yourIMG」)。show()',然後把'$(「#yourIMG」) .hide()'在你展示圖表的地方?此外,您似乎通過Ajax獲取圖表,爲什麼不隱藏圖像並在Ajax成功回調中顯示圖表? (使用延遲來給Ajax時間來完成並不是一個非常整潔或可靠的方法。) – nnnnnn

回答

0

如果您有已經工作了什麼,我想這只是一個展示,並在合適的地方隱藏的圖像元素的事:

$("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); 
});​