[clug] javascript jquery and long calls
Neill Cox
neill at ace-hosting.com.au
Wed Mar 30 01:12:05 MDT 2011
First let me admit that I don't really understand JavaScript's scoping rules
or closures, but until someone who know what they are talking about comes
along...
I think what's tripping you up is that in JavaScript functions are objects.
bar and setup are different objects and therefore "this" (which in
JavaScript is a keyword rather than an identifier) in each function resolves
to that function, meaning that they each have their own this.url,
this.waitForMessage etc.
x = function () { this.b = 'fred'; return this.b }
function () { this.b = 'fred'; return this.b }
y = function () { this.b = 'george'; return this.b }
function () { this.b = 'george'; return this.b }
z = { f_x:x, f_y:y }
Object
z.f_x
function () { this.b = 'fred'; return this.b }
z.f_x()
"fred"
z.f_y()
"george"
If you want to share information between them you will need to either pass
parameters or declare a global (or a variable in the enclosing scope)
Perhaps:
var bar = {
url:"",
textout:"",
setup: function(p_url, p_textout) {
url=p_url;
textout=p_textout;
waitForMessage();
},
waitForMessage: function() { ...}
}
or
function bar() {
var url;
var textout;
function setup(p_url, p_textout) {
url = p_url;
textout = p_textout;
waitForMessage();
}
function waitForMessage() {
...
}
}
This form begs the question of how you will get p_url and p_textout to the
setup function though. I guess bar.setup('http://example.com/gettext',
myArray) might work, but it looks odd.
Also is there a reason you are not setting the timeout using the option of
the jQuery Ajax object?
I have a feeling looking at this that either I don't understand what you are
up to, or there is likely to be a simpler way than what I have suggested.
Cheers,
Neill
On Wed, Mar 30, 2011 at 4:35 PM, jm <jeffm at ghostgun.com> wrote:
> I'm experimenting with comet a the moment using jquery. The problem I'm
> having is the variable 'this' doesn't appear to be in scope. What I'm
> trying to do in set up an object which stores various bits of information,
> eg this.url should be the URL to call and this.textout should be the div to
> write the messages to, this object has the method waitForMessage() as
> defined below. The expected behaviour is that I call it once, makes a call
> to the remote url, then falls through while waiting due to async being true,
> it then appends the returned text to the div, and calls itself.
>
> function bar() {
> this.setup = setup;
> this.waitForMessage = waitForMessage;
> }
>
> function setup(url, textout) {
> this.url = url;
> this.textout = textout;
> this.waitForMessage();
> }
>
> function waitForMessage() {
> $.ajax({
> url: this.url,
> async: true,
> cache: false,
> success: function(data){
> if (data.text) {
> for(var i = 0; i < data.text.length; i++) {
> this.textout.append($(data.text[i]));
> }
> }
> setTimeout('waitForMessage()', 1000);
> }
> });
> }
>
> Can anyone tell me how this SHOULD be written?
>
>
> Jeff.
>
>
> --
> linux mailing list
> linux at lists.samba.org
> https://lists.samba.org/mailman/listinfo/linux
>
More information about the linux
mailing list