1
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.

captainsac
  • 2,484
  • 3
  • 27
  • 48
  • 1
    Its not good practice to define functions within loops. It means function needs to be created in every iteration of loop. Better to define it outside of loop and then just call it in the loop. – Craicerjack May 29 '15 at 08:08
  • 1
    I've found, personally, that I've started to shy away from anonymous functions. When looking at console errors, its just nice to see the function name and have something to search for/jump to in the source code. Anonymous functions are just a *touch* harder to debug. – Jeremy J Starcher May 29 '15 at 08:27
  • @Craicerjack , I needed that loop because I was not defining a particular function , I am defining an array of functions. – Parnasree Chowdhury May 29 '15 at 09:21
  • Im not saying get rid of the loop. Im saying define the function outside of the loop and then push it into the array in the loop. That way you define the function once instead of on every iteration of the loop – Craicerjack May 29 '15 at 09:51
  • Yes, I got your point. Thanks for the explanation – Parnasree Chowdhury May 29 '15 at 20:11
  • I have discarded the idea of loop and actually used it at the time of calling the function and as @user3817416 has suggested I have prefered passing dynamic arguement to the function. – Parnasree Chowdhury May 29 '15 at 22:21

3 Answers3

1

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

Vasiliy vvscode Vanchuk
  • 7,007
  • 2
  • 21
  • 44
  • Ok, thanks for your reply.So I got that at some point I have to give a name to the function to call it. Is it ? – Parnasree Chowdhury May 29 '15 at 09:12
  • Almost - when you -- also you can get function by 'b' outside function - it works only inside -- check this comment. When you add name in such syntax - this name can be used only inside function - other code - can't use it – Vasiliy vvscode Vanchuk May 29 '15 at 11:26
1

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.

0

Call function inside setTimeout.check this

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

else

var myVar;

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

function alertFunc() {
   alert("Hello!");
}
AVM
  • 592
  • 2
  • 11
  • 25
  • Actually I wanted to call the same function within it , which is surely possible while the function has a name , just like in your example I could have written myVar = setTimeout(myFunction, 3000). But In my example it was an anonymous function. So I was wondered how could I get the same thing , how can I pass the anonymous function within that setTimeout() function. That's what my question . – Parnasree Chowdhury May 29 '15 at 09:15
  • Okay, `arguments.callee` is an option i think.Check this [fiddle](https://jsfiddle.net/gxoxn6cg/7/). – AVM May 29 '15 at 15:38
  • Pls don't use `arguments.callee` its deprecated.check [this](http://stackoverflow.com/questions/12050183/javascript-arguments-callee-what-is-it-for) – AVM May 29 '15 at 18:22
  • I have another question regarding this, will you please answer me? If I had a parameterized function, say I am passing an inetger value into the function and showing that value. So, in settimeout when I am calling the function how can I send parameter into that . ex: ` function myFunction(n){ alert(n); setTimeout(myFunction, 3000); }` Now I want to pass an argument from setTimeout into myFunction. How can I accomplish that? – Parnasree Chowdhury May 29 '15 at 20:53