0

in my html page i have div tag i'm print server side data hear.

<div id="chatbox">
   <label>${modelAttribute.chatMessage}<br></label>
</div>

First time it is working fine but i'm thinking each and every 10 sec this div will be called function.

so how to call each and every time particular method?

Hear i'm not click any thing

Please suggest me...

SWEE
  • 447
  • 2
  • 8
  • 14
  • 1
    you need to look at [AJAX](https://developer.mozilla.org/en/docs/AJAX) – Arun P Johny Jun 19 '13 at 08:36
  • possible duplicate of [Trying to call a function every 3 minutes using javascript](http://stackoverflow.com/questions/1491737/trying-to-call-a-function-every-3-minutes-using-javascript) – Pere Villega Jun 19 '13 at 09:11
  • Hey Guys.. Why this Question was Closed???? Answering People was Mad!!!! giving to Answer... This Question have Five answers.... – Java Developer Jun 20 '13 at 05:40

5 Answers5

2

Use setInterval(); method

you can find more here : http://www.w3schools.com/jsref/met_win_setinterval.asp

Rakesh Singh
  • 1,250
  • 9
  • 8
2

Use Ajax method with setInterval();. using this you can easily call function in some interval

R Singh
  • 21
  • 2
1

setInterval(function(){ //you can set an AJAX call inside },10000) will do the trick for you.

steo
  • 4,586
  • 2
  • 33
  • 64
1

You can refresh your div with all the data using setInterval()

<div id="chatbox">
//content
</div>

And the jquery to refresh the div:

<script>
    $(document).ready(function(){
        setInterval(function() {
            $("#chatbox").load("getchatbox.php #chatbox");
        }, 10000);
    });

</script>

You can also use it with ajax. a nice tutorial here: http://jquery-howto.blogspot.nl/2009/04/ajax-update-content-every-x-seconds.html Simple example using AJAX:

<script type="text/javascript">
jQuery(function($) {
function updateData()
{
var html = $.ajax({
url: "/getchatboxdata",
success: function(data) {
$('#chatbox').html(data);
}
});
}

updateData();

var auto_refresh = setInterval(function(){
updateData()
}, 10000);
});
</script>
Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106
  • +1 from me. A fair warning though: `setInterval`will cause stacking if the download takes more than 10 seconds (not likely here, but still a risk - bad/unstable connection). In that regard `setTimeout` is probably a better choice if 10 seconds accuracy is not strictly required (or you can end up with 2+ asynchronous ajax request at once in worse case). Assuming ajax here... –  Jun 19 '13 at 08:45
  • Thankyou.. is their is any plausibility to write direct function like onload in div tag.. – SWEE Jun 19 '13 at 08:45
  • 1
    Yep. you could also use AJAX. – Kees Sonnema Jun 19 '13 at 08:46
  • can you edit your code once with Ajax call? – SWEE Jun 19 '13 at 08:47
  • Yes there is. This function is called .append() You can call a function when the div is fully loaded. the the answer [here](http://stackoverflow.com/a/4160706/1379394) – Kees Sonnema Jun 19 '13 at 08:47
  • Updated my answer with a very simple ajax call. – Kees Sonnema Jun 19 '13 at 08:54
0

You can uset setInterval function on window onLoad and pass function which you want to call and time interval

window.onload = function() // Start your timer when the window has loaded
{
   setInterval("<function_name>;", <time_interval>); // Using milliseconds, define your 10 second interval
}

this will call your function periodically without clicking any where.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46