Delay does not work in setTimeout() and setInterval() within JavaScript widget

Service Center version 11.6.31

For my mobile app I could not get
the 'setTimeout()' and 'setInterval()' JavaScript functions
to delay the argument function invocation.

The parentheses with the function argument were causing
my problem:
                    setTimeout(funtionName(), 1000);
would fire 'functionName()' immediately, while:
                    setTimeout(functionName, 1000);
without parentheses/arguments, did wait 1s before calling.

Note: In the case of 'setInterval()', the function would also be called just once
          when parentheses were included in the function argument.


Same held for Client Actions as function arguments. The delays only worked
on Client Actions without mandatory input parameters, as in:
                 setTimeout($actions.DifferentAction, 1000);

and not in:

                 setTimeout($actions.DifferentAction("an argument"), 1000);

and, for the record, neither in:

                 setTimeout($actions.DifferentAction(), 1000);
.


Added as a Post.

Hi, maybe this is a typical issue with such asynchronous calls as with setInterval(), You can solve this using .bind()

maybe the code I made below can give you an idea of the way forward:


function printTest(param)

{        

console.log(param);

}


function exampleSetInterval(){    

     let varTest = "test 123";    

     setInterval(function (varTest) {       

          printTest(varTest);   

      }.bind(null, varTest), 1000);

}

exampleSetInterval();


Hope this helps ;)

Solution

HI PK,

You can pass parameters in setTimeout or setInterval using below syntax

function myFunction(str1, str2) {
  alert(str1); //hello
  alert(str2); //world
}

window.setTimeout(myFunction, 10, 'hello', 'world');

This should work with client action also.

If you place parentheses after function / client action name it will call it right away because this is the syntax to call a function. 

Solution

Colombelli wrote:

Hi, maybe this is a typical issue with such asynchronous calls as with setInterval(), You can solve this using .bind()

maybe the code I made below can give you an idea of the way forward:


function printTest(param)

{        

console.log(param);

}


function exampleSetInterval(){    

     let varTest = "test 123";    

     setInterval(function (varTest) {       

          printTest(varTest);   

      }.bind(null, varTest), 1000);

}

exampleSetInterval();


Hope this helps ;)

Dear Colombelli,

Yeah, I was struck by the fact that calling the target function in the 'function(){ }' wrapper
makes everything work!

Thank you!

So, I do not even have to use '.bind(...)', which also works... Thanks!
And the function-wrapper can also do without parameters, as in
    let varTest = "Thank you so much!";
    setInterval(function(){targetFunction(varTest);},1000);

Nikhil Gaur wrote:

HI PK,

You can pass parameters in setTimeout or setInterval using below syntax

function myFunction(str1, str2) {
  alert(str1); //hello
  alert(str2); //world
}

window.setTimeout(myFunction, 10, 'hello', 'world');

This should work with client action also.

If you place parentheses after function / client action name it will call it right away because this is the syntax to call a function. 

Thank you!