How to recall function under jquery plugin
I have created one jQuery plugin to use in my own system. The problem: I would like the system to update every 1 second. Normally I recall the function by use setTimeout
with the name of its function. But now when I use in jQuery it does not work. What's wrong with it and how can I solve it?
(function($){
$.fn.OnlineBox = function(options){
var user={};
var container = this;
user.id=$(container).children("id").text();
user.name=$(container).children("name").text();
user.sip=$(container).children("sip").text();
user.session=$(container).children("session").text();
user.ip=$(container).children("ip").text();
var func={
updateStatus : function(){
$.ajax({
url: "libs/online.php",
type: "POST",
data: {
"cmd" : "update",
"id" : user.id,
"name" : user.name,
"sip" : user.sip,
"session" : user.session,
"ip" : user.ip
},
dataType: "html",
success: function(msg){
str=msg.split(",");
$("#OnlineStatus").html(str[0]);
$("#OnlineScreen").html(str[1]);
}
})开发者_JS百科;
setTimeout("updateStatus()",1000);
}
}
func.updateStatus();
};
})(jQuery);
you can use the arguments.callee
, a reference to the current function
setTimeout(arguments.callee,1000);
There is no function named updateStatus
in there. There is, however, a func.updateStatus
function so your setTimeout
should look like this:
setTimeout(func.updateStatus, 1000);
Also, calling setTimeout
(or setInterval
) with a string as the first argument is generally considered bad form, you're better off passing a function as the first argument.
精彩评论