2013-02-04 102 views
1

我有一個問題,因爲我不是很熟練與js,我會很感激任何意見。如何臨時存儲數據與jQuery

基本上,我在每x秒執行的文章頁面上都有一個ajax調用,它連接到數據庫並檢查評論號。

現在,在每次通話之後,我需要檢查最後一次通話是否返回比前一次通話更大的號碼(以檢查號碼評論是否已增加)。

我很難弄清楚如何正確存儲單次調用的數據,這些數據在與下次調用比較後會被刪除,當然也會被新數據覆蓋。

現在,我有兩種方法可以使用並知道如何:在不可見元素內部將cookie和數據存儲在dom中,但這是最佳解決方案嗎?

+0

您應該顯示您到目前爲止所提供的代碼。僅供參考,因爲它只是AJAX一遍又一遍,你可以將數據保存在一個JS var ... – JAAulde

+1

謝謝JAAulde,我接受了下面的答案,但您的意見是正確的,謝謝:) – OriginalEXE

回答

5

你不需要使用cookie或dom來存儲這些數據。只需使用閉包*!

var old = null; 

$.get(url, function(d){ 
    if(old == null){ 
    old = d; 
    } 
    if(old.something > d.something){ 
     //do something else 
    } 
}); 

閉包是爲什麼這個作品,如果JavaScript的沒有他們,你將無法修改外old VAR在$.get功能的原因。

+0

Thnaks,由於某種原因,我擔心變量會在每個Ajax調用執行時自行清除。這實際上很簡單:) – OriginalEXE

0

你提到的兩種方法是正確的 - 我通常將數據存儲在DOM中的一個不可見元素內。

三個以上,你可能要考慮:

1)現代瀏覽器現在有了HTML5本地存儲空間,在功能上一個cookie的等價的,除非它存儲在瀏覽器中存儲。這在IE9中不存在,並且在現代瀏覽器中仍然在發展,所以除非你知道用戶的瀏覽器有這個功能(IE9不支持它),否則不要使用它。 2)如果您使用的是jQuery,則有jQuery數據:http://api.jquery.com/jQuery.data/,它允許您將數據附加到特定元素。這條鏈接比我能在這裏解釋得更好。

3)根據您的javascript設置的方式,您可以簡單地將以前評論的數量存儲在JavaScript中的變量中嗎?然後,當您的AJAX調用返回時,您可以比較結果變量與以前的計數變量。如果可能的話,我會選擇這個解決方案。

2

使用一個變量,你對此有何評論的範圍之外獲取/渲染功能:

var CommentsModule = (function(){ 
    var me = {}, 
     numComments = null; 

    function renderComments(comments){ 
     //render logic here 
     for(i=numComments || 0; i<comments.length;i++){ //the || 0 handles the initial load when the numComments === null 
      //append new comments 
     } 
     //reset your 'cached' variable to the current # of comments 
     numComments = comments.length; 
    } 

    me.getComments = function() { 
     //do $.get or $.ajax 
     $.ajax("myURL", 
      success: function(msg) { 
       if(msg.length > numComments) { 
        renderComments(msg); 
       } 
      } 
     }); 
    } 

    return me; 
}()); 

在你的頁面:

setInterval(CommentsModule.getComments, 5000); 
//....or something 
+0

謝謝,我接受上面的答案,因爲它之前發佈過,但這是正確的,我採用了這樣的解決方案。 – OriginalEXE

1

的localStorage:

localStorage.setItem('lastUpdate','2013-02-04'); 

var lastUpdate = localStorage.getItem('lastUpdate'); 

或使用您評論的數據屬性,例如:

$('#comments').data('lastUpdate', '2013-02-04'); 
0

您可以使用此代碼。它需要JQuery的cookie插件。它不適用於臨時存儲。

function GetData(key) 
    { 
     var sonuc = ""; 
     if (typeof (localStorage)!="undefined") 
     { 
      //İkinci html5 localStorage desteği varmı ona bakılır 
      if (localStorage[key] != null) 
      { 
       sonuc= localStorage[key]; 
      } 
     } 
     else 
     { 
      //son olarak cookie desteği varmı ona bakılır 
      sonuc=$.cookie(key); 
     } 
     return sonuc; 
    } 

    function SetData(key,value) 
    { 
     if (typeof (localStorage) != "undefined") 
     { 
      //ikinci önce html5 localStorage desteği varmı ona bakılır 
      try 
      { 
       localStorage.setItem(key, value); 
       return true; 
      } catch (e) 
      { 
       return false; 
      } 

     } 
     else 
     { 
      //son olarak cookie desteği varmı ona bakılır 
      try 
      { 
       $.cookie(key, value); 
       return true; 
      } catch (e) 
      { 
       return false; 
      } 
     } 
    } 

    /*this plugin is to add cookie function to jquery*/ 
    /*Copyright (c) 2006 Klaus Hartl (stilbuero.de)*/ 
    jQuery.cookie = function (name, value, options) { 
     if (typeof value != 'undefined') { // name and value given, set cookie 
      options = options || {}; 
      if (value === null) { 
       value = ''; 
       options.expires = -1; 
      } 
      var expires = ''; 
      if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { 
       var date; 
       if (typeof options.expires == 'number') { 
        date = new Date(); 
        date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); 
       } else { 
        date = options.expires; 
       } 
       expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE 
      } 
      var path = options.path ? '; path=' + (options.path) : ''; 
      var domain = options.domain ? '; domain=' + (options.domain) : ''; 
      var secure = options.secure ? '; secure' : ''; 
      document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); 
     } else { // only name given, get cookie 
      var cookieValue = null; 
      if (document.cookie && document.cookie != '') { 
       var cookies = document.cookie.split(';'); 
       for (var i = 0; i < cookies.length; i++) { 
        var cookie = jQuery.trim(cookies[i]); 
        // Does this cookie string begin with the name we want? 
        if (cookie.substring(0, name.length + 1) == (name + '=')) { 
         cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); 
         break; 
        } 
       } 
      } 
      return cookieValue; 
     } 
    }; 
    /*end of jquery plugin*/