2

I made this function:

function menucount(bartype, div){
    $(document).ready(function(){
        $.ajax({
            url: 'ajax.php',
            data:"opdate-menu="+bartype+"&bar=<?php echo $barnavn ?>",
            success: function(data) {
                $('#'+div).html(data);
            }
        });
   });
}

It checks for updates and displays a new number in my menu-buttons. I use it like this:

window.setInterval(menucount(), 2000);

This works.

Now I want to run this function on more buttons, so I create a 'wrapper' function and try to run it like this:

function wrapper(){
    menucount('manglerprint','printvip');
    menucount('manglerprint_plus','printvipplus');
    menucount('temp_vipplus','ansvipplus');
    menucount('temp_vipalm','ansvip');
}

window.setInterval(wrapper(), 2000);

But this doesnt work, It only runs one of the menucount() instances. How could I run menucount() many times like this?

Bolli
  • 4,974
  • 6
  • 31
  • 47

1 Answers1

2

You are invoking the function once using wrapper() and is passing the value returned by it undefined to the setInterval().

You need to pass the function reference to setInterval() like

window.setInterval(wrapper, 2000);

Note: Also the first example, that also will get executed only once...

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531