Showing posts with label repeat. Show all posts
Showing posts with label repeat. Show all posts

Thursday, 2 December 2010

How to set off multiple functions repeatedly using JavaScript

We are going to create an array of functions we'd like to kick off every 5 seconds. In this example I've used alerts, but this could make an excellent poll for changes to a file.

Once our array is created we can call a function which will perform the actions.

First we need to declare our function and stick it in the <head> of our page.
<script>

function runFunctions(funcArr, delay)
{
    setInterval(
        function()
        {
            for(i=0; i<funcArr.length; i++)
            {
                eval(funcArr[i]);
            }
        }, delay);
}

</script>

Now we can call our function from within the page <body> or just as easily in the <head>

<script>
            var myFuncs=new Array("alert('Mick')","alert('Mack')","alert('Paddy')","alert('Whack')");
            runFunctions(myFuncs, 5000);
</script>