[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

Calling the AJAX function

I have a simple AJAX form. I am calling the AJAX function in the Onclick event of a button.
Everything work well.

Now, what I want is I want this AJAX function executed every 5 seconds. How to do that?
The idea is to display the content of a text file from the server which is updated frequently.

For example, consider the hit counter which is stored in a text file, which is updated if any visitor enters the website home page. If i want to display the hit counter without refreshing the page, I need to use AJAX. This is just an example for you to understand what I want.

Any help is appreciated.
Thanks.
0
CWS (haripriya)
Asked:
CWS (haripriya)
  • 2
  • 2
  • 2
2 Solutions
 
Roger BaklundCommented:
Use the setInterval() function:

MyInterval = setInterval('LoadTextFile();',5000);

You can cancel it with clearInterval():

clearInterval(MyInterval);
0
 
CWS (haripriya)Author Commented:
I'm sorry, where I should call this in the body load?
0
 
Roger BaklundCommented:
Call it whereever/whenever you need to. You can for instance start it in a onclick event, or on document load. It will execute LoadTextFile() every 5 seconds untill clearInterval() is called.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
youssefomarCommented:
Here is how you do it

<html>
<head>
  <script language="javascript">
  	setInterval('LoadTextFile();',5000);
  </script>
</head>
<bod>
 
...
 
</body>
</html>

Open in new window

0
 
youssefomarCommented:
or you can use onLoad in the body tag

<html>
<head>
  ...
</head>
<body onload="setInterval('LoadTextFile();',5000);">
 
...
 
 
</body>
</html>

Open in new window

0
 
CWS (haripriya)Author Commented:
thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now