www.webdeveloper.com
Results 1 to 3 of 3

Thread: How do I call jQuery function every 3 seconds?

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Posts
    31

    How do I call jQuery function every 3 seconds?

    Suppose I have an html code :
    PHP Code:
    <html>
    <
    head>
    <
    script src="includejquery.js"/>
    <
    script type="text/javascript" src="chat.js"></script>
    </head>
    <title>Chat</title>
    <body onload="process()">
    <div id="chatmessages">
    </div>
    </body>
    </html> 
    Suppose I have a jQuery code(chat.js) :
    Code:
    function process(){
        $.get('chat.php', function(data) {
            $("#chatmessages").html(data+"<hr />");
        });
    });
    As you see its a very simple and basic code to implement chatting.
    And I intend to call the function process() every 3 seconds.
    What do I do to call the function process() every 3 seconds?

  2. #2
    Join Date
    Jul 2013
    Posts
    1
    declare a custom event with 'on' then trigger it with setInterval(). I am not a good code savvy. I think you are better coder than I. So please convert the logic into code. I will work.

  3. #3
    Join Date
    Jul 2013
    Posts
    31
    Quote Originally Posted by WordpressDev View Post
    declare a custom event with 'on' then trigger it with setInterval(). I am not a good code savvy. I think you are better coder than I. So please convert the logic into code. I will work.
    I just found out the solution. The jQuery code should be like this :
    Code:
    function process(){
        $.get('chat.php', function(data) {
            $("#chatmessages").html(data);
        });
        setTimeout(function(){
    	    process();},3000);
    }
    Its working perfectly with that.
    Thanks anyways.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles