[RESOLVED]validating functions

I am writing to seek help, to figure out, how can I test, to see, if the "sendNotifications", is being called or not?

<script src="~/Scripts/jquery-1.8.2.min.js" type="text/javascript" ></script>
<script src="~/Scripts/jquery.signalR-2.0.1.min.js" type="text/javascript" ></script>
<script src="~/signalr/hubs" type="text/javascript" ></script>
<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub.          
        var notifications = $.connection.NotificationHub;
        // Create a function that the hub can call to broadcast messages.
        notifications.client.recieveNotification = function (role, descrip) {
            // Add the message to the page.                    
            $('#spanNewMessages').text(role);
            $('#spanNewCircles').text(descrip);
            
        };
        // Start the connection.
        $.connection.hub.start().done(function () {
            notifications.server.sendNotifications();
        }).fail(function (e) {
            alert(e);
        });
        //$.connection.hub.start();
    });
</script>

Please advice further.  Many thanks.

Add an alert to inside the sendNotifications function.

Thank you for your reply.  I have managed to insert a alert code in "sendNotification" but I am not getting any response on client-side.

   // Start the connection.
        $.connection.hub.start().done(function () {
            notifications.server.sendNotifications(function () {
                alert("send noticiation");
                });
        }).fail(function (e) {
            alert(e);
        });
        //$.connection.hub.start();
    });

Please advice further, if possible.

Hi missy786,

You can put alert() some functions to see which works and which doesn’t works.

But right approach is to use Dev tool to debug and see what happens behind.

Review following posts about IE F12 and Mozila inspector:

http://msdn.microsoft.com/en-us/library/ie/gg589512%28v=vs.85%29.aspx

https://developer.mozilla.org/en-US/docs/Tools/Page_Inspector

Thanks

There are a few ways to handle issues like this (generic Javascript debugging) : 

  • Javascript Alerts
  • The debugger; keyword
  • Developer Tools (F12)

Javascript Alerts

The easiest way to see if a particular function or area of code is being called would be to use a
Javascript alert() function. Simply place the alert on the line that you want to generate a message on and if one appears, then your code is being hit :

$.connection.hub.start().done(function () {
      // Wrap this area in alerts to see if either section of it is being executed
      alert("Preparing to send notifications...");
      notifications.server.sendNotifications();
      alert("Notifications have been sent.");
}).fail(function (e) {
      alert(e);
});

The debugger; keyword

Another great way to debug Javascript code is to use the
debugger keyword
, which will allow you to step through your code, line-by-line as it executes (similar to the Visual Studio debugger). You’ll just need to include a debugger; line within your code as seen below :

$(function () {
        // Define a debugger
        debugger;

        // Declare a proxy to reference the hub.          
        var notifications = $.connection.NotificationHub;
        // Create a function that the hub can call to broadcast messages.
        notifications.client.recieveNotification = function (role, descrip) {
            // Add the message to the page.                    
            $('#spanNewMessages').text(role);
            $('#spanNewCircles').text(descrip);
            
        };
        // Start the connection.
        $.connection.hub.start().done(function () {
            notifications.server.sendNotifications();
        }).fail(function (e) {
            alert(e);
        });
        //$.connection.hub.start();
});

And then press F12 to open up the Developer Tools within your browser and refresh the page. It should hit the debugger line (like a breakpoint) and you can use F10-F11 to step through your code and follow it as it executes.

Developer Tools (F12)

The previously mentioned Developer Tools (accessible through the F12 key in most browsers) can be one of the best tools for troubleshooting any client-side issues. You can use it to view the Console (which will contain any errors that are present)
and use the Network tab to monitor any requests that are being made (which can be very useful with SignalR-related code).

Leave a Reply