2016-10-03 38 views
0

我試圖在沒有任何用戶事件的情況下連續更改html,只是時間。我試過這個:在沒有任何用戶事件的情況下連續更改div的html

$('#content').html('<p>Hello</p>').fadeIn(800).delay(1800).fadeOut(800); 
$('#content').delay(3400).html('<p>How are you?</p>').fadeIn(800).delay(1800).fadeOut(800); 
$('#content').delay(6800).html('<p>Great over here</p>').fadeIn(800); 

該延遲適用於效果,但顯示的文本總是最後一個。 我該怎麼做?

+1

使用'SetTimeOut' –

回答

1

所提議的卡菲基恩謝卡爾,它很容易使用setTimeout()

$('#content').html('<p>Hello</p>'); 
setTimeout(function(){$('#content').html('<p>How are you?</p>');},3400); 
setTimeout(function(){$('#content').html('<p>Great over here</p>')},6800); 
1

你或許可以嘗試這樣的事情:

function addTimoutElement(htmlContent, delayMs, isFadeout){ 
    setTimeout(function(){ 
     $('#content').html(htmlContent).fadeIn(800); 
     if(isFadeout){ 
      $('#content').delay(1800).fadeOut(800); 
     } 
    }, delayMs); 
}  

$('#content').html('<p>Hello</p>').fadeIn(800).delay(1800).fadeOut(800); 

addTimoutElement('<p>How are you?</p>', 3400, true); 
addTimoutElement('<p>Great over here</p>', 6800, false); 
+0

我認爲這是第一註釋解決。該函數已經作爲'SetTimeOut'存在。 這不是一個更好的選擇嗎? – CarlosAS

+0

我正在使用setTimeout(),但也使用一個函數,它會把你的新文本放在參數中。我也可以注意到淡出是你只在某些元素上做的事情。有了這些觀點,我認爲使用自定義函數是一個更好的標準。 –

相關問題