hjuster
hjuster

Reputation: 4070

Passing functions as a parameter

How to pass a function to another function and call it in that other script in specific moment? For example I would like to do something like this:

var interval;   
var parallaxElements = function(elementSet,operacija){
    clearInterval(interval);
    var parallax = [];
    if(elementSet!=null){
        elementSet.find(".parallax").each(function(index) {
            parallax.push($(this));
        }); 
    }   
    parallaxTiming = 30;
    parallax = _.shuffle(parallax);

    interval = setInterval(function(){
        var elem = parallax.shift();        
        if(typeof(elem)!="undefined"){
             elem.stop().transition({
                y: "0px",
                z: "0px",
                duration:"850ms"
             })  
        }else{
            clearInterval(interval);

THIS IS WHERE I EXPECT THE PASSED FUNCTION TO BE CALLED

            return operacija;
            };          
        }, parallaxTiming); 
    }

And then to call the function like this:

parallaxElements(container,function(){ alert(123); });

Any help will be more then usefull. Thanks in advance for all answers.

Upvotes: 0

Views: 62

Answers (3)

redbmk
redbmk

Reputation: 4796

Change return operacija; to return operacija();

Upvotes: 2

Cyrille Ka
Cyrille Ka

Reputation: 15533

Just call operacija().

Little demo code:

var foo = function(operacija) {

  interval = setInterval(function(){
    clearInterval(interval);
    return operacija();
  }, 1000);
};

foo(function() { alert("123"); } );

Upvotes: 4

Axel
Axel

Reputation: 142

In my opinion You just return the function as a function but not the result of its execution.

Try this:

return operacija.call();

Upvotes: 0

Related Questions