我希望能夠做document.write的一件事。然後延遲半秒,然後document.write更多。你知道這是可能的嗎?如果是這樣,怎麼樣?我已經試過這個到目前爲止,它沒有工作:如何document.write然後再延遲document.write
document.write("Hello!");
setTimeout(function(){
},1000);
document.write("hello!");
我希望能夠做document.write的一件事。然後延遲半秒,然後document.write更多。你知道這是可能的嗎?如果是這樣,怎麼樣?我已經試過這個到目前爲止,它沒有工作:如何document.write然後再延遲document.write
document.write("Hello!");
setTimeout(function(){
},1000);
document.write("hello!");
你需要把該語句裏面的setTimeout這樣。
document.write("Hello!");
setTimeout(function(){
document.write("hello!");
},1000);
第一個參數在setTimeout()是你要延遲毫秒後執行的功能。
下面是一個例子jsfiddle(不使用document.write)
您需要添加一些毫秒,以查看差異。 1000毫秒=1秒
你NEDD添加一些代碼到你的setTimeout函數,因爲這樣:
setTimeout(function(){console.log("Hello")},3000);
的setTimeout是不是一個 「暫停」。它會在一段時間後執行自己的代碼。
請看下面的例子:http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing1
你可以試試這個:
var docWriteLater = function(){
document.write("Hello Later!");
}
setTimeout(docWriteLater, 1000)
document.write("hello now!");
這將覆蓋隨後加載的頁面。 – Bergi
如果可能的話,不要做'document.write',這是避之唯恐不及的技術(元素添加到正文使用jQuery,例如:'$('body')。append('
Hello!
')')。 – 2013-06-01 19:53:10@herby爲什麼不是原生的'document.body.appendChild(document.createTextNode('Hello!'))'? –
@PaulS。無論通過API修改DOM。我不使用(也沒有時間和精力)學習本地API,因爲跨瀏覽器很棘手;我依賴於庫(也就是說,jQuery/zepto對我來說已經足夠了,它可以很好地實現)。 – 2013-06-01 19:57:02