-1

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).

Pablo
  • 1,373
  • 16
  • 36
  • 1
    As always, if anyone has any reasons to downvote or otherwise disapprove, I would appreciate some feedback. – Pablo Oct 04 '17 at 14:59

2 Answers2

1

The C# expression @Url.Action("KeepSessionAlive", "Auxiliary") will return a string, which is the url to the endpoint. So when you call the LaunchHeartBeat method, you need to pass it in quotes

LaunchHeartBeat('@Url.Action("KeepSessionAlive", "Auxiliary")');

Also setTimeout exceute only once. If you want something to be executed continuously you need to use setInterval

This should work

var isSuccess = false;
function LaunchHeartBeat(actionUrl) {
    setInterval(function() {
        $.ajax({
            type: "POST",
            url: actionUrl,
            success: function () { isSuccess = true; }
        });
    }, 2000);
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thanks for your time! The quotes were there before (I removed them while trying stuff). I will fix that, check if it solves the issue, and then edit the question here. I will use setInterval, but shouldn't the call CheckToKeepSessionAlive() at the end of KeepSessionAlive() create a loop as well? – Pablo Oct 04 '17 at 15:14
  • Let me try what you did. – Pablo Oct 04 '17 at 15:25
  • Still not working, but I think your code should be good. I set up a breakpoint in your function and it is not triggered; is it possible that I am doing something wrong in the html file? (This is my first time trying javascript code in its own file.) – Pablo Oct 04 '17 at 15:33
  • 1
    Check your browser console and see whether you have any script errors. If yes fix that. When you have some javascript not wokring, that is the way to start. Then comment out some part of your code and see that resolves it, if yes, you now know which part has bad code. – Shyju Oct 04 '17 at 15:41
  • I have a feeling there is something quite silly I am missing somewhere. I'll try to fetch a colleague tomorrow... in any case, thanks for your time, I learnt something from your answer even if it did not solve my problem. I will update if I figure out what I'm doing wrong. – Pablo Oct 04 '17 at 16:07
  • I just wanted to thank you for your help. Being new to web programming and javascript I would not have thought of using the browser console, which quickly led me to a fix. And in the way you also managed to improve my code, so kudos to you! – Pablo Oct 05 '17 at 08:48
0

Using the browser console revealed the error message:

Uncaught TypeError: Cannot read property 'ajax' of undefined

This means jQuery is not defined, which is solved by including the line

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

in the HTML file. (I learnt this last part in the answer to this question.)

Pablo
  • 1,373
  • 16
  • 36