[英]jQuery “Microsoft JScript runtime error: Object expected”
I have the below code that does not seem to work at all :( I keep getting: 我有下面的代码似乎根本不起作用:(我一直得到:
Microsoft JScript runtime error: Object expected
The error seems to occur when the timeout is done. 超时完成时似乎发生错误。 So if I raise the timeout with 10 seconds the error holds for another 10 seconds.
因此,如果我将超时提高10秒,则错误持续10秒。
I want to be able to update the number of friends online async. 我希望能够在线异步更新朋友的数量。 The number is shown with the following html:
该数字显示如下html:
<a href="" id="showChat" >Friends online <strong id="friendsOnline">(?)</strong></a>
The friends part is set at the first run, but when the timeout calls back it does not fire again. 朋友部分是在第一次运行时设置的,但是当超时回调时,它不会再次触发。 Also, I cannot see on which line the error occurs because if I want to break on the error it just shows "no source code" etc.
此外,我无法看到错误发生在哪一行,因为如果我想打破错误它只显示“没有源代码”等。
The code 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>
Try this: 尝试这个:
window.setTimeout(UpdateFriendsOnline, 1000);
The version you had would have worked if the function was defined in the global namespace. 如果在全局命名空间中定义了该函数,那么您所使用的版本将起作用。
This way, you're passing in a local reference to the function, which will get called every second. 这样,您将传递对该函数的本地引用,该函数将每秒调用一次。
EDIT: 编辑:
If you need to cancel the previous request before the new one starts, 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>
Change your setTimeout()
like this: 像这样更改你的
setTimeout()
:
window.setTimeout(UpdateFriendsOnline, 1000);
Currently your function isn't available outside the document.ready
, so it's not accessible as a global function, which passing it as a string is trying to access it as. 目前你的函数在
document.ready
之外是不可用的,所以它不能作为全局函数访问,它将它作为字符串传递尝试访问它。 As a general rule, never ever pass setTimeout()
a string if you can avoid it...it can cause problems like this case, and I can't think of an example (that if avoidable) is made better by it being a string. 作为一般规则,从来没有过
setTimeout()
一个字符串,如果你能避免它...它可能会导致这样的情况下的问题,我想不出一个例子由它作为一个(如果可以避免的)制成更好串。
Also, I suggest firing it when you get the response back, otherwise you'll start queuing up overlapping ajax requests, you can do that by adjusting your function to this: 此外,我建议你在收到回复时触发它,否则你将开始排队重叠ajax请求,你可以通过调整你的函数来做到这一点:
function UpdateFriendsOnline() {
$.get("/Account/GetFriendsOnline", function(data) {
$("#friendsOnline").html("(" + data + ")");
window.setTimeout(UpdateFriendsOnline, 1000);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.