• JS OOP Skullduggery

    From Angus McLeod@VERT/ANJO to All on Monday, October 02, 2006 01:16:00
    Here's a good one!

    You create an object with a method, like so:

    function myobj_looper() {
    . . .
    . . .
    timer_id = setTimeout( "myobj_looper()", 1000 );
    }

    function myobj_start() {
    this.looper();
    }

    function myobj() {
    . . .
    . . .

    this.start = myobj_start;
    this.looper = myobj_looper;
    }

    Now, you instantiate an object, and start the looper, like so:

    someobj = new myobj();
    someobj->start();

    The start() method uses setTimeout() to call the looper routine. The
    looper routine uses setTimeout(), to call itself every second.

    Only trouble is, it doesn't work.

    I've never tried using setTimeout() within an object method before. I've tried booth of these:

    timer_id = setTimeout( "myobj_looper()", 1000 );
    timer_id = setTimeout( "this.looper()", 1000 );
    timer_id = this.setTimeout( "looper()", 1000 );

    neither of which seem to work at all. Neither one generates any errors,
    but looper() doesn't seem to run, other than the first time when called initially from start(). Since setTimeout() returns an "opaque value" for timer_id, and there doesn't seem to be any way of determining the success
    of the setTimeout() call, I am only *assuming* that it is failing, but I
    can't see any other reasonable explanation.

    So anybody got any suggestions on the proper way to call setTimeout() with
    an object method as the code argument?

    ---
    Playing: "Lunar orbit five - Spanish castles in space" by "The Orb"
    from "The Orb's Adventures Beyond The Ultraworld" album
    þ Synchronet þ Programatically generated on The ANJO BBS
  • From Tracker1@VERT/TRN to Angus McLeod on Monday, October 02, 2006 01:11:00
    sorry for top posting...
    ugly, but here goes...

    Presuming this is meant for a browser...

    function myobj_looper() {
    var w = this.worker;

    //do something

    if (this.worker != null && this.worker == w)
    this.worker = window.setTimeout(
    "window['" + this.objectID + "'].looper()",
    1000
    );
    }

    function myobj_start() {
    if (this.worker == null) {
    this.worker = Math.random();
    this.looper();
    }
    }

    function myobj_stop() {
    this.worker = null;
    }

    function myobj() {
    this.objectID = Math.random();
    window[this.objectID] = this;
    this.worker = null;
    this.working = false;

    this.looper = myobj_looper;
    this.start = myobj_start;
    this.stop = myobj_stop;
    }

    someobj = new myobj();
    someobj.start();


    Angus McLeod wrote:
    Here's a good one!

    You create an object with a method, like so:

    function myobj_looper() {
    . . .
    . . .
    timer_id = setTimeout( "myobj_looper()", 1000 );
    }

    function myobj_start() {
    this.looper();
    }

    function myobj() {
    . . .
    . . .

    this.start = myobj_start;
    this.looper = myobj_looper;
    }

    Now, you instantiate an object, and start the looper, like so:

    someobj = new myobj();
    someobj->start();

    The start() method uses setTimeout() to call the looper routine. The
    looper routine uses setTimeout(), to call itself every second.

    Only trouble is, it doesn't work.

    I've never tried using setTimeout() within an object method before. I've tried booth of these:

    timer_id = setTimeout( "myobj_looper()", 1000 );
    timer_id = setTimeout( "this.looper()", 1000 );
    timer_id = this.setTimeout( "looper()", 1000 );

    neither of which seem to work at all. Neither one generates any errors,
    but looper() doesn't seem to run, other than the first time when called initially from start(). Since setTimeout() returns an "opaque value" for timer_id, and there doesn't seem to be any way of determining the success
    of the setTimeout() call, I am only *assuming* that it is failing, but I can't see any other reasonable explanation.

    So anybody got any suggestions on the proper way to call setTimeout() with an object method as the code argument?

    --
    Michael J. Ryan - tracker1(at)theroughnecks(dot)net - www.theroughnecks.net icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

    ---
    þ Synchronet þ theroughnecks.net - you know you want it
  • From Angus McLeod@VERT/ANJO to Tracker1 on Monday, October 02, 2006 09:03:00
    Re: Re: JS OOP Skullduggery
    By: Tracker1 to Angus McLeod on Mon Oct 02 2006 01:11:00

    sorry for top posting...
    ugly, but here goes...

    Presuming this is meant for a browser...

    Ok, thanks! I'll have a go at it in a minute.


    ---
    Playing: "Free man in Paris" by "Joni Mitchell"
    from the "Court & spark" album
    þ Synchronet þ Programatically generated on The ANJO BBS
  • From Tracker1@VERT/TRN to Angus McLeod on Monday, October 02, 2006 21:18:00
    Angus McLeod wrote:
    sorry for top posting...
    ugly, but here goes...

    Presuming this is meant for a browser...

    Ok, thanks! I'll have a go at it in a minute.

    yeah, it gets kind of ugly, put a little check in to be able to stop
    and restart, the objectID is mainly for being able to assign/reference
    from the window object... intervals & timeouts in JS/browser are a bit
    of a real pain... at least doing a lock-like setting is relatively easy
    as js is all called in the same thread (not always in order) though.

    Look into the Venkman javascript debugger for firefox/mozilla... will help a lot when having to step through the garbage.. ;)

    --
    Michael J. Ryan - tracker1(at)theroughnecks(dot)net - www.theroughnecks.net icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

    ---
    þ Synchronet þ theroughnecks.net - you know you want it
  • From Angus McLeod@VERT/ANJO to Tracker1 on Wednesday, October 04, 2006 00:31:00
    Re: Re: JS OOP Skullduggery
    By: Tracker1 to Angus McLeod on Mon Oct 02 2006 21:18:00

    Ok, thanks! I'll have a go at it in a minute.

    yeah, it gets kind of ugly, put a little check in to be able to stop
    and restart, the objectID is mainly for being able to assign/reference
    from the window object... intervals & timeouts in JS/browser are a bit
    of a real pain... at least doing a lock-like setting is relatively easy
    as js is all called in the same thread (not always in order) though.

    I stole out your

    this.objectID = Math.random();
    window[this.objectID] = this;

    code for the constructor, and use

    timeout_code = "window[" + this.objectID + "].new_slide()";
    timer_id = setTimeout( timeout_code, duration );

    in the looper() function. I had to mke a change or two here and there.

    It also works with explicitly assigned numeric values for this.objectID (I tried my telephone number and that worked fine). What I was NOT able to achieve was the use of a string objectID -- which would have been
    convenient for various reasons. I tried

    this.objectID = 'some-random-string';
    window[this.objectID] = this;

    in the constructor, and

    timeout_code = "window['" + this.objectID + "'].new_slide()";
    timer_id = setTimeout( timeout_code, duration );

    in the looper() code (note extra single-quotes) but this completely failed
    to work. I can't think why this would fail, but then I never seem to get
    to work on this until after midnight, so maybe I'm just missing something obvious.

    ---
    Playing: "Find the cost of freedom" by "Crosby, Stills, Nash & Young"
    from the "So far" album
    þ Synchronet þ Programatically generated on The ANJO BBS
  • From Angus McLeod@VERT/ANJO to Tracker1 on Wednesday, October 04, 2006 00:45:00
    Re: Re: JS OOP Skullduggery
    By: Angus McLeod to Tracker1 on Wed Oct 04 2006 00:31:21

    What I was NOT able to achieve was the use of a string objectID...

    Heh! Just tried this again, and of course, it worked fine. I guess last
    time I tried it I was too cross-eyed weary to see whatever obvious typo I included that made it fail.

    Now if only I could find a way to determine the name of the instance of a particular class instance....
    ---
    Playing: "Guinnevere" by "Crosby, Stills, Nash & Young"
    from the "So far" album
    þ Synchronet þ Programatically generated on The ANJO BBS
  • From Angus McLeod@VERT/ANJO to Tracker1 on Thursday, October 05, 2006 01:44:00
    Re: Re: JS OOP Skullduggery
    By: Tracker1 to Angus McLeod on Mon Oct 02 2006 21:18:00

    <sigh> I'm giving up on this project. I just can't seem to get the time
    to get it sorted, and I'm tired of burning the long-past-midnight oil on
    it. Here's what I've got:

    function myobj_looper() {
    timeout_code = "window[" + this.objectID + "].looper()";
    if (this.stopped)
    timer_id = setTimeout( timeout_code, 100 ); // check again soon
    } else {
    // do stuff here
    }
    }

    function myobj_activate() {
    this.stopped = false;
    this.looper();
    }

    function myobj_stopit() {
    this.stopped = true;
    }

    function myobj_startit() {
    this.stopped = false;
    }

    function myobj() {
    this.objectID = Math.random();
    this.stopped = false;
    // other constructor-type stuff here

    this.looper = myobj_looper;
    this.activate = myobj_activate;
    this.stopit = myobj_stopit;
    this.startit = myobj_startit;
    }

    Now, I declare an instance of this class in my page like so:

    inst = new myobj();

    and I start the looper in the BODY tag like

    <body onload="inst.activate();">

    the object runs and the looper() does it's job fine. So activate() and looper() are functioning as expected. But I also use

    <div onmouseover='inst.stopit();' onmouseout='inst.startit();'>

    with the intention of pausing the looper() when the mouse slides over the contents of the DIV. Unfortunately, the looper() does not pause, and the
    JS Console says:

    inst.stopit is not a function
    inst.startit is not a function

    whenever the mouse enters (or exits) the area defined by the DIV. I can
    not quite figure out how looper() and activate() -- along with other
    methods used by myobj -- are found and work, while startit() and stopit()
    are not known. Why would functions in a BODY tag (and in a SCRIPT block)
    work, while functions in a DIV tag fail?

    ---
    Strange: With 10,292 tracks available from 800 albums
    by 401 artists, I'm currently not listening to anything!
    þ Synchronet þ Programatically generated on The ANJO BBS
  • From Angus McLeod@VERT/ANJO to Tracker1 on Thursday, October 05, 2006 02:05:00
    Re: Re: JS OOP Skullduggery
    By: Tracker1 to Angus McLeod on Mon Oct 02 2006 21:18:00

    I dunno how you do it, but as soon as I tell you about a piece of code
    that *will* not work, no matter what I do... it immediately starts working without my having to do anything to make it!

    The startit() and stopit() methods now work fine. And I haven't changed a thing!

    Oh well...

    ---
    Playing: "Feel the same way" by "Saigon Kick" from "The lizard" album
    þ Synchronet þ Programatically generated on The ANJO BBS
  • From Tracker1@VERT/TRN to Angus McLeod on Thursday, October 05, 2006 02:13:00
    Angus McLeod wrote:
    It also works with explicitly assigned numeric values for this.objectID (I tried my telephone number and that worked fine). What I was NOT able to achieve was the use of a string objectID -- which would have been
    convenient for various reasons. I tried

    Hmmm.. what about having the objectID as a string, and using eval to use
    or assign it? not sure.. again, I haven't had to do it in a while, and
    it's always a pita to have self-referencing object events..

    In flash, what I usually do is have a "handler" with an array/stack of
    todo things, and add my methods to the stack, returning true, or false
    to indicate if it's "done" (and remove the event/method from the stack
    when something is done).

    --
    Michael J. Ryan - tracker1(at)theroughnecks(dot)net - www.theroughnecks.net icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

    ---
    þ Synchronet þ theroughnecks.net - you know you want it
  • From Tracker1@VERT/TRN to Angus McLeod on Thursday, October 05, 2006 02:14:00
    Angus McLeod wrote:
    What I was NOT able to achieve was the use of a string objectID...

    Heh! Just tried this again, and of course, it worked fine. I guess last time I tried it I was too cross-eyed weary to see whatever obvious typo I included that made it fail.

    Now if only I could find a way to determine the name of the instance of a particular class instance....

    LOL, wouldn't that be nice... I've done this with tree (parent/child) type objects, by passing the "name" assigned as part of the constructor...

    --
    Michael J. Ryan - tracker1(at)theroughnecks(dot)net - www.theroughnecks.net icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

    ---
    þ Synchronet þ theroughnecks.net - you know you want it
  • From Tracker1@VERT/TRN to Angus McLeod on Friday, October 06, 2006 02:20:00
    Angus McLeod wrote:
    I dunno how you do it, but as soon as I tell you about a piece of code
    that *will* not work, no matter what I do... it immediately starts working without my having to do anything to make it!

    The startit() and stopit() methods now work fine. And I haven't changed a thing!

    Oh well...

    If the js was in an external file, could be a browser caching issue...
    for firefox, see about:config for browser.cache.check_doc_frequency

    for development, I set this to 1 (every time)

    --
    Michael J. Ryan - tracker1(at)theroughnecks(dot)net - www.theroughnecks.net icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

    ---
    þ Synchronet þ theroughnecks.net - you know you want it
  • From Angus McLeod@VERT/ANJO to Tracker1 on Friday, October 06, 2006 02:39:00
    Re: Re: JS OOP Skullduggery
    By: Tracker1 to Angus McLeod on Thu Oct 05 2006 02:14:00

    Now if only I could find a way to determine the name of the instance of a particular class instance....

    LOL, wouldn't that be nice...

    Yes. :-) But I don't think it's gonna happen, tho.

    I've done this with tree (parent/child) type objects, by passing the
    "name" assigned as part of the constructor...

    What I've been doing is creating and manipulating PHP objects which do the server-side grunt-work (accessing MySQL, or whatever), and then I do
    something like $phpobj->emit_JS(); which writes the apropriate JS to
    create the client-side JS objects. This can emit calls to constructors
    with very complicated argument lists, or a simpler constructor followed by
    a series of calls to additional methods to set other properties of the JS objects. I'm passing the objectID to the PHP constructor, and emit_JS() propagates the same objectID when it generates the JS constructor.

    I'm tweaking the CSS as well, but I'm experimenting with generating object specific stile blocks, generating inline style-'' attributes to the HTML elements in question, or using a pre-defined class and then issuing JS commands to alter the CSS for specific elements as needed. I've yet to
    decide which approach I like best.
    ---
    Playing: "Model" by "Simply Red" from the "Stars" album
    þ Synchronet þ Programatically generated on The ANJO BBS
  • From Tracker1@VERT/TRN to Angus McLeod on Friday, October 06, 2006 20:33:00
    Angus McLeod wrote:
    What I've been doing is creating and manipulating PHP objects which do the server-side grunt-work (accessing MySQL, or whatever), and then I do something like $phpobj->emit_JS(); which writes the apropriate JS to
    create the client-side JS objects. This can emit calls to constructors
    with very complicated argument lists, or a simpler constructor followed by
    a series of calls to additional methods to set other properties of the JS objects. I'm passing the objectID to the PHP constructor, and emit_JS() propagates the same objectID when it generates the JS constructor.

    Isn't there already a JSON module/library for php?

    I'm tweaking the CSS as well, but I'm experimenting with generating object specific stile blocks, generating inline style-'' attributes to the HTML elements in question, or using a pre-defined class and then issuing JS commands to alter the CSS for specific elements as needed. I've yet to decide which approach I like best.

    You may want to give a look at the following.. :)

    JavaScript Object Notation (JSON): http://www.json.org/
    JavaScript OOP - http://phrogz.net/JS/Classes/OOPinJS.html
    Prototype - http://prototype.conio.net/
    Behavior - http://bennolan.com/behaviour/

    --
    Michael J. Ryan - tracker1(at)theroughnecks(dot)net - www.theroughnecks.net icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

    ---
    þ Synchronet þ theroughnecks.net - you know you want it
  • From Angus McLeod@VERT/ANJO to Tracker1 on Friday, October 06, 2006 23:54:00
    Re: Re: JS OOP Skullduggery
    By: Tracker1 to Angus McLeod on Fri Oct 06 2006 20:33:00

    Isn't there already a JSON module/library for php?

    yes, but it isn't available on the server that the code will eventually
    reside on.

    You may want to give a look at the following.. :)

    JavaScript Object Notation (JSON): http://www.json.org/
    JavaScript OOP - http://phrogz.net/JS/Classes/OOPinJS.html
    Prototype - http://prototype.conio.net/
    Behavior - http://bennolan.com/behaviour/

    Yes, thanks. I've had a look.

    ---
    Playing: "Shit Towne" by "Live" from the "Throwing Copper" album
    þ Synchronet þ Programatically generated on The ANJO BBS