jquery
2010-09-21 92 views 2 likes 
2

我的想法起初是這是一個語法問題,但我沒有看到任何語法問題。我已經添加調試代碼,這給了奇怪的結果,x登錄之前jQuery('#notification')執行setTimeout後元素列表後的Javascript錯誤「丟失」

document.triggerNotification = function (type, message) { 
    jQuery(document.body).append("<div class='push-notification push-"+type+"' id='notification'>"+message+"</div>"); 

    setTimeout(jQuery('#notification').fadeOut(1200, function() { 
     console.log(jQuery('#notification')); 
     jQuery('#notification').remove(); 
     console.log(jQuery('#notification')); 
    }), 3000); 
    console.log('x'); 
} 

螢火蟲提供了以下的輸出:

x 
[div#notification.push-notification] 
[] 
missing ] after element list - [Break on this error] [object Object] 

一切都成功執行,但它仍然拋出一個錯誤。

回答

10

setTimeout需要一個函數作爲它的第一個參數。你給它一個jQuery對象的集合。請嘗試以下操作:

document.triggerNotification = function (type, message) { 
    jQuery(document.body).append("<div class='push-notification push-"+type+"' id='notification'>"+message+"</div>"); 

    setTimeout(function() { jQuery('#notification').fadeOut(1200, function() { 
     console.log(jQuery('#notification')); 
     jQuery('#notification').remove(); 
     console.log(jQuery('#notification')); 
    })}, 3000); 
    console.log('x'); 
} 

注意,已纏你jQuery('#notification').fadeOut()調用匿名函數。用你當前的代碼,我期望fadeOut立即執行,而不是在指定的3秒後執行。

+1

不要忘記刪除'console.log()' – Sam 2010-09-21 14:51:45

 相關問題

  • 暫無相關問題^_^