whats the total number of ns in:
q: how to call a function within settimeout? i have this code in my lightning component helper where i want to call another method form the same helper. 
window.settimeout(
  $a.getcallback(function() {
    for(var key in somefieldsets){ 
      if(somefieldsets[key].fieldapiname == samplefield__c && !$a.util.isempty(somefieldsets[key].fieldvalue)) {
        this.secondfunction(component);
      }
    }
  }), 2000
);

calling this.secondfunction(component); works when it is outside the settimeout but when inside it, it doesnt perform the secondfunction anymore. what is the other option of calling a function within the helper? why does this doesnt work this time?

a: this will not work in settimeout. you need to pass that using bind. check link for understanding.

a: in order to use asynchronous functions setinterval and settimeout you need to bind the function passed to those methods to the current context (that is: your component itself).
your component might be referenced all the time in your code using this, especially when accessing and changing its own attributes.
to bind the function to your current context, use .bind(this) on the function object. remember: functions are first-class objects, which means that when you call a function the interpreter will return you a representation of it (an object). and that way you can call the .bind method.
so if your function is called dosomethinginonesecond, for example, and you are calling it from a method in your component, youll use it like this:
method() {
    settimeout(function() {
        console.log(hello one second after!)
    }.bind(this), 1000)
}

note: the new(ish) js syntax of using arrows => requires you to wrap your function with parenthesis:
settimeout((() => {
    console.log(hello one second after!)
}).bind(this), 1000)


user
The text includes 126 "n"s in total.
assistant