virtualsets
virtualsets

Reputation: 403

setTimeout and kineticjs

I want to fire objects with a timeout. The problem is that the below code only fires the first object. Does anybody know how I can fire in diferent times the tweens?

function _tween_out(_objeto){
    _objeto.play();
}
function _tween_in(_objeto){
    _objeto.reverse();
}
myVar=[]
bk.on('mouseenter', function(evt) { 

for (var j=0;j<iconos_bases.length;j++){
        var mandar=iconos_bases[j].group.tween
        myVar[j]=setTimeout(function(){_tween_out(mandar)},getRandomInt(1,100));
    }
    stage.draw();

});

I change the code.

 function _tween_out(_objeto){
    _objeto.play();
}
function _tween_in(_objeto){
    _objeto.reverse();
}
myVar=[]
bk.on('mouseenter', function(evt) { 

for (var j=0;j<iconos_bases.length;j++){
        var mandar=iconos_bases[j].group.tween
        var num=getRandomInt(1,600000);
        console.log(num);
        setTimeout(_tween_out(mandar),num);
    }

    stage.draw();

});

but all the timeout in the same time.

Upvotes: 0

Views: 172

Answers (1)

markE
markE

Reputation: 105035

One thing: How much time delay are you calculating here?

// 600000 seems like a large number ??

var num=getRandomInt(1,600000);

Anyway...

A Demo: http://jsfiddle.net/m1erickson/CTRuq/

In setTimeout you can't send arguments with your _tween_out function.

// This Won't Work !

setTimeout(_tween_out(mandar),num);

If you want to send an argument you must instead give setTimeout an anonymous function:

setTimeout( function(){ _tween_out(mandar); }, num );

Upvotes: 1

Related Questions