Skip to main content
Inspiring
November 24, 2006
Question

Scoping Problem

  • November 24, 2006
  • 2 replies
  • 259 views
quote:

function sendMsg(s)
{
trace(s);
}

application.onConnect = function() { setInterval(sendMsg, 1000, "test"); }


When using code like that, setInterval can't reach sendMsg()... I know this seems like an incredibly newbie question... but how do I make it work?

I can't seem to figure out the scope of toplevel functions -- never needed to do this because everything else is tucked neatly in classes and it's mostly remoting...
    This topic has been closed for replies.

    2 replies

    November 24, 2006
    We are using the same sort of code like you are using, seraerie, so I don't see the problem there.

    1. define a function to fire at the onConnect event
    2. start an interval
    3. let the interval call a global defined function

    Nothing wrong with that.

    It's a fact THAT you cannot see the difference INSIDE the sendMsg between all connected clients, that IS a serious problem. To solve this make use of the clientObject:

    function sendMsg(paramObject) {

    // paramObject.client holds a reference to the client(object) so you can use paramObject.client.ip to show the IP-address of the user

    // prepend some text to the traceoutput for readability
    trace(" sendMsg: message = "paramObject.message);
    trace(" sendMsg: ip of the client = "paramObject.client.ip);


    }

    application.onConnect = function(clientObject) {

    // define a new object to hold my parameters
    paramObject = new Object();
    paramObject.client = clientObject;
    paramObject.message = "test";

    // define an interval and pass our object filled with parameters (much easier than appending a lot of parameters on 1 line
    setInterval(sendMsg, 1000, paramObject);
    }
    Inspiring
    November 24, 2006
    Hello :)

    you can test this code and the second syntax to used setInterval with
    scope and method name with a string representation :



    Timer = {} ;

    /**
    * The id interval of this timer.
    */
    Timer.idInterval = null ;

    /**
    * The delay of the interval.
    */
    Timer.delay = 1000 ; // default value

    Timer.sendMessage = function( msg /*String*/ )
    {
    trace("msg : " + msg) ;
    }

    Timer.delayMessage = function( msg /*String*/ , delay /*Number*/ )
    {
    this.stopTimer() ;

    if ( !isNaN(delay) )
    {
    this.delay = (delay > 0) ? delay : 0 ;
    }

    setInterval(this, "sendMessage", this.delay, msg );
    }

    Timer.stopTimer = function()
    {
    if (this.idInterval != null)
    {
    clearInterval(this.idInterval) ;
    this.idInterval = null ;
    }
    }

    application.onConnect = function()
    {
    Timer.delayMessage( "hello world..." , 2000) ;
    }



    EKA+ :)

    >
    quote:

    function sendMsg(s)
    > {
    > trace(s);
    > }
    >
    > application.onConnect = function() { setInterval(sendMsg, 1000, "test"); }

    >
    > When using code like that, setInterval can't reach sendMsg()... I know this
    > seems like an incredibly newbie question... but how do I make it work?
    >
    > I can't seem to figure out the scope of toplevel functions -- never needed to
    > do this because everything else is tucked neatly in classes and it's mostly
    > remoting...
    >