[Solved-5 Solutions] How can we pass a parameter to a setTimeout() callback ? - javascript Tutorial
Problem:
In JavaScript code should be like:
An error will occurs on topicId is not defined. This code is working before using this setTimeout()function. After some time we call postinsql (topicId) fuction.
How to Solve this Problem ?
Solution 1:
In this example, we also output the parameters that were passed to the alertFunc() function. This does not work in IE9 and earlier version.
Solution 2:
The "setTimeout" receives a third parameter that is sent as parameter to the internal function at the end of the timer.
This solution we have some research and testing, they given an implementation:
setTimeout will pass all extra parameters to your function so they can be processed. The anonymous function can be work. We want to use "this", within instance of a object that function will not be work. Any anonymous function will change "this" to point to window, so we will lose your object reference.
Solution 4:
We can pass as many parameters to the function called by setTimeout.