Javascript help

Asked

Viewed 52 times

1

How do I get this script to run in 10 seconds ??

<script type = "text/javascript">

function simulateClick(x, y) {

var clickEvent = document.createEvent('MouseEvents');
clickEvent.initMouseEvent('click', true, true, window, 0, 0, 0, x, y, false, false, false, false, 0, null);
document.elementFromPoint(x, y).dispatchEvent(clickEvent);

}
simulateClick(1176, 435);
</script> 

The idea would be to put a Countdown on it, someone knows ?

  • 1

    setTimeout(simulateClick(1176,435), 10000);

  • 3

    @That would make him execute immediately, only the result (undefined) would be passed on to setTimeout. Better would be setTimeout(function() { simulateClick(1176,435); }, 10000);

  • @mgibsonbr worked perfect, thanks

  • 1

    @mgibsonbr truth, what to write in a hurry. Write an answer there to earn internet points ;)

  • @Caiofelipepereira Blz, I did it. And I still learned something new rsrs (I did not know that the setTimeout accepted additional arguments - in browsers recent at least)

1 answer

5

The function setTimeout can be used to invoke a function after X milliseconds:

var timeoutID = setTimeout(simulateClick, 10000, 1176, 435);

The first parameter is the function that will be called, the second the number of milliseconds to wait (10s = 10000ms). The additional parameters are passed directly to the function, that is, it is almost the same as doing:

setTimeout(function() { simulateClick(1176,435); }, 10000);

(Note: if you want to support IE9 or earlier always use this second format, because the first is not supported)

The return value can be ignored if you don’t need it. It is used if you want cancel the function call before it occurs, via clearTimeout(timeoutID).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.