I was recently suggested to try heartbeats to solve an issue in my project. Because I am new to javascript, I was trying to implement the second answer to this question before moving on, but even this does not seem to work, and I can't understand why. These are the three files involved:
HTML (Heart.cshtml)
<head>
<script type="text/javascript" src="~/Scripts/heartBeat.js"></script>
</head>
<body>
<script type="text/javascript">
LaunchHeartBeat('@Url.Action("KeepSessionAlive", "Auxiliary")');
</script>
</body>
Javascript (heartBeat.js)
var isSuccess = false;
function LaunchHeartBeat(actionUrl) {
setInterval(function() {
$.ajax({
type: "POST",
url: actionUrl,
success: function () { isSuccess = true; }
});
}, 2000);
}
Controller (AuxiliaryController.cs)
[HttpPost]
public JsonResult KeepSessionAlive()
{
System.Diagnostics.Debug.WriteLine("This is a heart beat");
return new JsonResult { Data = "success" };
}
Sorry for the (I believe) simple question. I would have asked in a comment, but I don't have enough reputation yet, so I did not know how else to do it.
Edit: I made some edits in my code to make some corrections/simplifications as suggested by the first answer. It did not solve my problem but the code is nicer now. I have also removed some unnecessary lines from the HTML file (which don't affect the question, but now the code is cleaner and easier to understand at a glance).