ajax
次のような小さなリソースのチェックを事前に使用できます。
$(document).ready(function() {
setInterval(maybeRefresh, 66000);
function maybeRefresh() {
$.ajax({
url: "/path/to/tiny/resource",
type: "GET",
success: function() {
// That worked, do the full refresh
location.reload();
}
});
}
});
load
...しかし、代わりに更新されたコンテンツを使用したほうがよいと思う傾向があります。すべてのプライマリコンテンツを、を使用して要素に配置しid
"main"
、次に:
$(document).ready(function() {
setInterval(function() {
$("#main").load("/path/to/new/content");
}, 66000);
});
#main
これにより、要素のコンテンツがtoから受信したGET
コンテンツに置き換えられ/path/to/new/content
ます。失敗したGET
場合、更新はありません。
setTimeout
私はおそらく、aの代わりにチェーンを使用し、setInterval
より積極的に更新しようとすることでエラーを処理します。何かのようなもの:
$(document).ready(function() {
setTimeout(refresh, 66000);
function refresh() {
$.ajax({
url: "/path/to/new/content",
type: "GET",
success: function(html) {
// Success, update the content
$("#main").html(html);
// Reload in 66 seconds
setTimeout(refresh, 66000);
},
error: function() {
// Failed, try again in five seconds
setTimeout(refresh, 5000);
}
});
}
});