how to pass the dynamic variable to setInterval method

前端 未结 2 1396
礼貌的吻别
礼貌的吻别 2021-01-27 07:54

i want to refresh/reload the method in every 30 seconds. but im not able to send the variable data to another function i.e setInterval.how to pass variable in the setInterval me

2条回答
  •  無奈伤痛
    2021-01-27 08:18

    Courtesy of @tvanfosson:

    You probably want to have a function that creates the interval timer for you. Pass in the parameter to the function so its value is captured in the function closure and retained for when the timer expires.

    function createInterval(f,dynamicParameter,interval) { setInterval(function() { f(dynamicParameter); }, interval); }
    

    Then call it as createInterval(funca,dynamicValue,500); Obviously you can extend this for more than one parameter. And, please, use more descriptive variable names. :)

提交回复
热议问题