'SetTimeout functionality not taking place

In the a.js file I have a function called as:

Move(coordinates,callback) {
//Move the cursor from point A to point B based on the coordinates as parameter
});

In the b.js file I am calling this function since I want to see the cursor move slowly after every 30 seconds. This is the call:

for(i=0;i<6;i++)
{
setTimeout(Move(inputcordinates,events = {
  //someevents
}),30000);
}

Problem is when the page is loaded , the function is getting executed and the cursor has already moved 6 times to the position, The delay is not happening in front of my eyes but the functionality is executed for sure.

What could be the problem in the code shown above? I have increased the code to about 120000 also still no effect.



Solution 1:[1]

It is because you call your function directly. You must write it like this :

setTimeout(function() {
    Move(inputcordinates,events = {
       //someevents
    });
}, 30000)

Solution 2:[2]

You can also use setInterval for this which is easy to achieve and clear Try as below..

 var i = 0;
 var myVar;
        function getData() {

            myVar = setInterval(function () { Move(x,y)() }, 2000);

        }
        function Move(coordinates,callback){
            i++;
            alert('Hi');
            if (i == 5) {
                alert('5 times');
                clearInterval(myVar);
            }

        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Magus
Solution 2