如何使用ajax每分钟刷新一次页面



我想每15秒用页面上的匹配分钟更新一个div。我该怎么做?我只想刷新div所在的区域。

<script>
setInterval(function () {
$.ajax({
@*url: '@Url.Action("_List", "Home",new { match_id=Model.match.match_id})',*@
cache: false,
success: function (result) {
$("#test").html(result);
console.log(result)
//alert(result);
}
});
}, 20000);
</script>

为此使用局部视图。它们将允许您只更新DOM的一部分,而不必执行全页刷新或回发,并且它们是强类型的。

例如,我在下面创建了部分视图

function loadPartialView() {
$.ajax({
url: "@Url.Action("ActionName", "ControllerName")",
type: 'GET', // <-- make a async request by GET
dataType: 'html', // <-- to expect an html response
success: function(result) {
$('#YourDiv').html(result);
}
});
}
$(function() {
loadPartialView(); // first time
// re-call the function each 5 seconds
window.setInterval("loadPartialView()", 5000);
});

每5秒后,它进入控制器并执行动作

public class ControllerName: Controller
{
public ActionResult ActionName()
{
.
.   // code for update object
.
return PartialView("PartialViewName", updatedObject);
}
}

有关详细信息https://cmatskas.com/update-an-mvc-partial-view-with-ajax/

最新更新