2013-09-01 27 views
0

當我開始使用此代碼段自動刷新與阿賈克斯塊的衝突與發生「像」 ING活動上的BuddyPress:使用自定義自動刷新的js代碼,使衝突

myscr.js

jQuery(document).ready(function() { 
    function update() { 
     jQuery("#notice").html('Updating...'); 
     jQuery.ajax({ 
     type: 'GET', 
     url: 'http://domain.com/activity', 
     data: "recentac=true", 
     //timeout: 5000, 
     success: function(data) { 
      jQuery("#recent-activities").html(data); 
      jQuery("#notice").html(''); 
      window.setTimeout(update, 20000); 
     }, 
     error: function (XMLHttpRequest, textStatus, errorThrown) { 
      jQuery("#notice").html('Error in connection'); 
      window.setTimeout(update, 5000); 
     } 
    }); 
    } 
    update(); 
}); 

而且我用wp_enqueue_script打印我的腳本:

function auto_refresh() 
{ 
    wp_enqueue_script('myscr', get_template_directory_uri().'/myscr.js', array("jquery"), '1.0', true); 
} 
add_action('wp_enqueue_scripts', 'auto_refresh', 99); 

自動refereshing作品和我注意到一個一個前uto刷新,「像」ing作品,之後它不!控制檯也不會顯示任何關於它的錯誤。

任何幫助將不勝感激。

回答

0

嘿我不知道這是否會有所幫助,但我已閱讀了一些沒有衝突,這是它在鏈接下說的,我複製了大部分內容,還有額外的好東西在鏈接上閱讀,信息。希望這有助於

站點鏈接:http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/

把jQuery的進入無衝突模式 當你把jQuery的到無衝突模式,用戶可以在同一個新的變量名來代替$別名的選項。

<!-- Putting jQuery into no-conflict mode. --> 
<script src="prototype.js"></script> 
<script src="jquery.js"></script> 
<script> 

var $j = jQuery.noConflict(); 
// $j is now an alias to the jQuery function; creating the new alias is optional. 

$j(document).ready(function() { 
    $j("div").hide(); 
}); 

// The $ variable now has the prototype meaning, which is a shortcut for 
// document.getElementById(). mainDiv below is a DOM element, not a jQuery object. 
window.onload = function() { 
    var mainDiv = $("main"); 
} 

</script> 

在上面的代碼中,$將恢復到它在原始庫中的含義。您仍然可以在應用程序的其餘部分使用完整的函數名稱jQuery以及新的別名$ j。新的別名可以命名爲任何你想要的:jq,$ J,awesomeQuery等。

最後,如果你不想定義另一個替代完整的jQuery函數名稱(你真的喜歡用$並不關心使用其他庫的$方法),那麼還有另一種方法可以嘗試:只需將$作爲傳遞給您的jQuery(document).ready()函數的參數。如果你仍然想要真正簡潔的jQuery代碼的好處,但不想引起與其他庫衝突的情況,這是最常用的。

<!-- Another way to put jQuery into no-conflict mode. --> 
<script src="prototype.js"></script> 
<script src="jquery.js"></script> 
<script> 

jQuery.noConflict(); 

jQuery(document).ready(function($) { 
    // You can use the locally-scoped $ in here as an alias to jQuery. 
    $("div").hide(); 
}); 

// The $ variable in the global scope has the prototype.js meaning. 
window.onload = function(){ 
    var mainDiv = $("main"); 
} 

</script>