/    Sign up×
Community /Pin to ProfileBookmark

How do I call jQuery function every 3 seconds?

Suppose I have an html code :

[code=php]<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>[/code]

Suppose I have a jQuery code(chat.js) :

[code]
function process(){
$.get(‘chat.php’, function(data) {
$(“#chatmessages”).html(data+”<hr />”);
});
});[/code]

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?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@WordpressDevJul 14.2013 — 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.
Copy linkTweet thisAlerts:
@The_AlchemistauthorJul 14.2013 — 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.[/QUOTE]
I just found out the solution. The jQuery code should be like this :
function process(){
$.get('chat.php', function(data) {
$("#chatmessages").html(data);
});
setTimeout(function(){
process();},3000);
}

Its working perfectly with that.

Thanks anyways.
×

Success!

Help @The_Alchemist spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...