arguments.callee.task

Marco Lorenzin's icon

Hi,

If you want to access the Task from within a function, is there a reason
why should you use the syntax arguments.callee.task, rather than using
the variable name you assigned to the Task object?

For example, is there a difference between the first and the second example?

// example 1
var tsk = new Task(mytask, this);
function mytask
{
    arguments.callee.task.cancel();
}

// example 2:
var tsk2 = new Task(mytask2, this);
function mytask2
{
    tsk2.cancel();
}

---
Marco

Peter Castine's icon

Came across this while looking for something else. In case anyone cares:

Example 2 won't work if you use an anonymous function.

var tsktsk = new Task(function() { post("this function is has no namen"); });

I believe this functionality is mentioned in the documentation without a concrete example.

DRKOSS's icon

Question on this - does this mean you can't schedule an anonymous function? If not - how would you feed a defined function different arguments?

example:

         var offset = 10;
var noteOut = new Task(function(){
                 messnamed("synth", 144,(24+offset),127);
                 });
noteOut.schedule(1000);

do.while's icon

feed it as an object Drkoss

DRKOSS's icon

@DO...WHILE - thanks for the reply!

can you post a short example to point me in the right direction? I'm sure I'm missing the obvious here - but how would i 'feed it as an object'?

thanks!

do.while's icon

yes sure .


var state = {value : 0};

var tick = function(st){
      post(st.value);
      st.value++;
      arguments.callee.task.schedule(266); 
}

var clock = new Task(this,undefined,state);

U define object , and pass it as a third argument . this way u can affect properties of an object . It will work with all reference types , like an array too .

DRKOSS's icon

aha - thanks!

one question - it that a typo - should it read:

var clock = new Task(tick,undefined,state);

instead of

var clock = new Task(this,undefined,state);

again - many thanks!

do.while's icon

yes . sorry . "tick" for sure

DRKOSS's icon

For some reason - doing that way acts like repeat - it will keep counting up instead of executing once. For anyone interested - this works for a single execution:

var state = {value : 0};

var tick = function(st){
post(st.value);
st.value++;
}

var clock = new Task(tick,undefined,state);

clock.schedule(500);

cheers!

DRKOSS's icon

One problem - this doesn't seem to work if the the var state is dynamic. It seems to define the function when compiled and no longer updates when executed later on (when state has become a different value)

any ideas?

thanks!

do.while's icon

Your code would help here as i did not encountered any problems on my own .

var state = {value : 0};

var tick = function(st){
    post(st.value+"\n");
    st.value++;
    arguments.callee.task.schedule(500);
}

var clock = new Task(tick,this,state);


function bang(){
    clock.execute();
}

function msg_int(v){
    state.value = v ;
}

.
While setting value for the state object ,it will reflect inside tick function . I cant see reason why it would not work, unless u are setting your object to undefined . Treat your object as an argument holder .post scriptum: dont be shocked while its repeating . its for the example purposes .