有没有办法绕过javascript上的缓存

Is there a way to bypass cache on javascript?

本文关键字:缓存 javascript 有没有      更新时间:2023-09-26

我正在尝试显示我放入iframe:的每个源的加载时间

$.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());
});

有没有一种方法可以让它不依赖于缓存?

您可以使用$.ajax函数的cache属性:

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

它会自动在URL的末尾附加一个时间戳参数。