setInterval() only running function once

后端 未结 6 1715
遇见更好的自我
遇见更好的自我 2020-11-30 12:07

I want to periodically query a PHP script for new messages. To do so, I\'m using the setInterval() function and AJAX.

$(document).ready(function(){

    var          


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 12:25

    Check your code line:

    setInterval(getMessages(), queryInterval);
    

    The setInterval function requires you to pass the reference to your callback function.

    When you pass getMessages(), You are actually calling the function and passing its returning object to the setInterval function.

    So just change your line to:

    setInterval(getMessages, queryInterval);
    

    and it will works fine!

提交回复
热议问题