JQuery "Microsoft JScript Runtime Error: Expected Object"

I have the code below that doesn't seem to work at all :( I keep getting:

Microsoft JScript runtime error: Object expected 

An error occurs when the timeout is completed. Therefore, if I raise the timeout with 10 seconds, the error will continue for another 10 seconds.

I want to be able to update the number of friends on the Internet. The number is displayed with the following html:

 <a href="" id="showChat" >Friends online <strong id="friendsOnline">(?)</strong></a> 

Some friends are established at the first start, but when the timeout calls back, it does not start again. Also, I don’t see which line the error occurs on, because if I want to break the error, it just shows β€œno source code”, etc.

Below is the code I'm using. Thanks!

 <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.js" type="text/javascript"></script> <script src='/Scripts/MicrosoftAjax.js' type="text/javascript"></script> <script src='/Scripts/MicrosoftMvcAjax.js' type="text/javascript"></script> <script src='/Scripts/jquery.autocomplete.js' type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { UpdateFriendsOnline(); function UpdateFriendsOnline() { window.setTimeout("UpdateFriendsOnline()", 1000); $.get("/Account/GetFriendsOnline", function(data) { $("#friendsOnline").html("(" + data + ")"); }); } }); </script> 
+7
javascript jquery c #
source share
2 answers

Change setTimeout() as follows:

 window.setTimeout(UpdateFriendsOnline, 1000); 

Your function is currently not available outside of document.ready , therefore it is not available as a global function, passing it as a string, trying to access it. As a rule, never skip the setTimeout() line if you can avoid it ... it can cause problems like this case, and I cannot come up with an example (which, if it can be avoided), is improved due to the fact that it is line.

In addition, I suggest dismissing it when you receive an answer, otherwise you will start queues with overlapping ajax requests, you can do this by setting your function to this:

 function UpdateFriendsOnline() { $.get("/Account/GetFriendsOnline", function(data) { $("#friendsOnline").html("(" + data + ")"); window.setTimeout(UpdateFriendsOnline, 1000); }); } 
+3
source share

Try the following:

 window.setTimeout(UpdateFriendsOnline, 1000); 

The version you had would work if the function were defined in the global namespace.

Thus, you pass a local link to a function that will be called every second.


EDIT:

If you need to cancel the previous request before starting a new one, you can do something like this:

 <script type="text/javascript"> $(document).ready(function() { var request; // Stores XMLHTTPRequest object UpdateFriendsOnline(); function UpdateFriendsOnline() { if(request) request.abort(); // Abort current request if there is one window.setTimeout(UpdateFriendsOnline, 1000); // Store new XMLHTTPRequest object request = $.get("/Account/GetFriendsOnline", function(data) { request = null; // Clear request object upon success $("#friendsOnline").html("(" + data + ")"); }); } }); </script> 
+3
source share

All Articles