2013-02-26 94 views
0

我想,以顯示我投入的iframe每個源的加載時間:有沒有辦法繞過JavaScript上的緩存?

$.get("text.txt", function (data) { 
    var array = data.split(/\r\n|\r|\n/) 
    var beforeLoad = (new Date()).getTime();  
    var loadTimes = []; 

    $('#1').on('load', function() { 
     loadTimes.push((new Date()).getTime());   

     $('#1').attr('src', array.pop()); 

     $.each(loadTimes, function (index, value) {    
      var result = (value - beforeLoad)/1000;  
      $("#loadingtime" + index).html(result); 
     }); 
    }).attr('src', array.pop()); 
}); 

有沒有辦法以某種方式讓這個它難道不依賴於緩存?

+0

使用適當的[緩存標題](http://stackoverflow.com/questions/1046966/whats-the-difference-between-cache-control-max-age-0-and-no-cache) – 2013-02-26 05:27:33

+1

提示: ID不應該從數字開始。 '#1'不知何故不正確。閱讀:https://developer.mozilla.org/en/docs/HTML/Global_attributes – diEcho 2013-02-26 05:29:37

+0

到底什麼是#1? – 2013-02-26 05:29:55

回答

3

可以使用$.ajax功能的cache屬性:

$.ajax({ 
    type: 'GET', 
    url: 'text.txt', 
    cache: false, 
    success: function() { 
     ... 
    } 
}); 

這將時間戳參數自動添加到您的網址的結尾。

+0

OP想要緩存但你設置了'cache:false'。這樣對嗎? – diEcho 2013-02-26 05:32:12

+2

OP說「* wouldnt取決於緩存*」,所以我雖然這意味着禁用緩存。 – Blender 2013-02-26 05:33:17

相關問題