Calling a jQuery plugin inside itself
Posted
by
Real Tuty
on Stack Overflow
See other posts from Stack Overflow
or by Real Tuty
Published on 2012-06-17T21:13:21Z
Indexed on
2012/06/17
21:16 UTC
Read the original article
Hit count: 223
I am trying to create a comet like thing. I have a plugin that collects data from a php page. The problem is that i dont know how to call the plugin inside itself.
If it were a function i could go like this: function j () {setTimeout(j(), 1000);}
, but i am using a jQuery plugin.
Here is my plugin code:
(function($) {
$.fn.watch = function(ops) {
var
$this_ = this,
setngs = $.extend({
'type' : 'JSON',
'query' : 'GET',
'url' : '',
'data' : '',
'wait' : 1000
}, ops);
if (setngs.type === '') {
return false;
} else if (setngs.query === '') {
return false;
} else if (setngs.url === '') {
return false;
} else if (setngs.wait === '') {
return false;
} else if (setngs.wait === 0) {
setngs.wait = 1000;
}
var xhr = $.ajax({
type : setngs.query,
dataType : setngs.type,
url : setngs.url,
success : function(data) {
var i = 0;
for (i = 0; i < data.length; i++) {
var html = $this_.html(), str = '<li class="post" id="post-' + data[i].id + '"><div class="inner"><div class="user">' + data[i].user + '</div><div class="body">' + data[i].body + '</div></div></li>';
$this_.html(str + html);
}
setTimeout($this_, 1000);
}
});
};
})(jQuery);
where it says setTimeout($this_, 1000);
this is where im having trouble. I don't know what to call the plugin as. $this_
is what I thought might work but I am wrong. That is what i need to replace.
Thanks for your help.
© Stack Overflow or respective owner