setInterval does not seem to be working?

2019-03-04 04:10发布

问题:

This code does not seem to be working. I am trying to create a dynamic list of chat rooms available that updates every 10 seconds. I also want the user to be able to set peraminters to filter what rooms would be shown. I am using this code, and it does not seem to be working for some reason.

<script type="text/javascript"> 
function setRooms()
{
    console.log('ok');
    if($('#only').is(':checked'))
    {
     var checked = 1;   
    }
    else 
    {
    var checked = 0;    
    }
    if($('#open').is(':checked'))
    {
     var opened  = 1;   
    }
    else 
    {
    var opened = 0; 
    }
    $.post('backend/outputRooms', {
        fLevel : $('#flevel').val(), 
        sLevel : $('#slevel').val(),
        display : $('display').val(),
        Checked : checked,

        }, 
        function(data)
        {
            $('#text').html(data);
        }); 
}
$(document).ready(function(){
    setInterval(setRooms(), 10000);
    });
</script>

I have ruled out a problem with the backend page, as the console.log('ok') does not seem to be working as well. Any help?

回答1:

Try putting quotes around your function:

setInterval('setRooms()', 10000);

Alternatively change your method to:

var setRooms = function()

and you can then use:

setInterval(setRooms, 10000);


回答2:

setInterval(setRooms, 10000);