Parnasree Chowdhury
Parnasree Chowdhury

Reputation: 344

How to call a function within it while its an anonymous function

var scaleJssor = new Array();
for(myloop=0; myloop<jssor_slider.length; myloop++)
{
  scaleJssor.push(
        function() { 
             var parentWidth = jssor_slider[myloop].$Elmt.parentNode.clientWidth;

             if (parentWidth)
                {
                    //alert("executing ScaleSlider");
                    jssor_slider[myloop].$SetScaleWidth(Math.max(Math.min(parentWidth-100, 960), 230));
                }
             else
                    window.setTimeout(<here I want to pass this function>, 30);

                });

}

The above is only a code snippet , I just want to know how can I pass the function within setTimeout() function where the function is anonymous, I know if I give a name to the function I can pass it easily, but I want a way by not giving any name to function.

Upvotes: 1

Views: 130

Answers (3)

Julius Mallik
Julius Mallik

Reputation: 71

Though this is not a good enough code because you are defining same function over and over in the loop. You should have defined the function outside the loop . I am just answering you for the question of setTimeout() function. The following may do a good thing to work with if you pass a parameter to your function.

function ScaleJssor(k) { 
             var parentWidth = jssor_slider[k].$Elmt.parentNode.clientWidth;

             if (parentWidth)
                {
                    //alert("executing ScaleSlider");
                    jssor_slider[k].$SetScaleWidth(Math.max(Math.min(parentWidth-100, 960), 230));
                }
             else
                    window.setTimeout(function(){ScaleJssor(k)}, 30);

                }

I think if you use the above code in your example you need not to use the loop anyway.

Upvotes: 1

Vasiliy vvscode Vanchuk
Vasiliy vvscode Vanchuk

Reputation: 7169

you can use the next

someCode(function funcName() {
  // here you can access to function with funcName

});

this is works for next cases

var a = function b() {
  // here your function can be access with a or b
  // but 'b' garantee you that you call current function
  // and 'a' not - cause at code below you can redefine a-value to another one
  // also you can get function by 'b' outside function - it works only inside
}

Check it here http://jsbin.com/rakupayaxo/1/edit?js,output

Upvotes: 1

AVM
AVM

Reputation: 592

Call function inside setTimeout.check this

setTimeout(function(){ alert("Hello");    }, 3000);

else

var myVar;

function myFunction() {
   myVar = setTimeout(alertFunc, 3000);
}

function alertFunc() {
   alert("Hello!");
}

Upvotes: 0

Related Questions