Results 1 to 4 of 4

Thread: setInterval initial value

  1. #1
    Join Date
    Jan 2007
    Posts
    58
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default setInterval initial value

    Hello,

    I have the next script to get some info from the database, it refreshes itself every 5 seconds, but, every time I reload the page I have to wait 5 secs just for it to show, I know i can go to #online for example and place there the initial value, but I would like to do it with javascript, seems more clean.

    Code:
    <script type="text/javascript">
    $(function() {
    
    	setInterval(function() {
    		$.getJSON('include/ajaxStats.php', function (data) {
    		  $('#online').html(data.online);
    		  $('#accts').html(data.accts);
    		  $('#chars').html(data.chars);
    		});
    	}, 5000);
    
    });
    </script>
    Also if this code can be improved don't doubt on let me know, I'm just a newbie.

    Thanks much!
    Last edited by nicksalad; 10-30-2009 at 08:17 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not clear on where data is coming from, unless it is from $.getJSON itself. But if it works as an interval, it should work as a function, so try:

    Code:
    $(function() {
    	function myFunc() {
    		$.getJSON('include/ajaxStats.php', function (data) {
    		  $('#online').html(data.online);
    		  $('#accts').html(data.accts);
    		  $('#chars').html(data.chars);
    		});
    	}
    	myFunc();
    	setInterval(myFunc, 5000);
    
    });
    I'm assuming this is jQuery here and that the wrapper is the shorthand form of the document.ready function.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    nicksalad (10-28-2009)

  4. #3
    Join Date
    Jan 2007
    Posts
    58
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    I'm not clear on where data is coming from, unless it is from $.getJSON itself. But if it works as an interval, it should work as a function, so try:

    Code:
    $(function() {
    	function myFunc() {
    		$.getJSON('include/ajaxStats.php', function (data) {
    		  $('#online').html(data.online);
    		  $('#accts').html(data.accts);
    		  $('#chars').html(data.chars);
    		});
    	}
    	myFunc();
    	setInterval(myFunc, 5000);
    
    });
    I'm assuming this is jQuery here and that the wrapper is the shorthand form of the document.ready function.
    Yes it is jQuery, sorry for not mention it, and on the php side looks like this:

    Code:
    <?php
    
    require_once('config.php');
    require_once('stats.php');
    
    $accts = accounts_total();
    $chars = chars_total();
    $on = online_total();
    $lim = $GLOBALS['cfg']['maxvirtualonline'];
    
    echo '{online: '.$on.', accts: '.$accts.', chars: '.$chars.'}';
    
    ?>
    Can it be improved? It works.. but dunno if is good hehe.

    Regarding your post, it works like a charm, thanks again! you are a life savior.

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Looks pretty straightforward to me, but I'm no expert at PHP.

    Incidentally, on the javascript side, you my alternatively use/return the arguments.callee property of a self executing anonymous function if you like:

    Code:
    <script type="text/javascript">
    $(function() {
    
    	setInterval((function() {
    		$.getJSON('include/ajaxStats.php', function (data) {
    		  $('#online').html(data.online);
    		  $('#accts').html(data.accts);
    		  $('#chars').html(data.chars);
    		});
    		return arguments.callee;
    	})(), 5000);
    
    });
    </script>
    Which may appeal to the jQuery mindset more than my first solution does. Though I believe the first solution is a slight bit more efficient.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. The Following User Says Thank You to jscheuer1 For This Useful Post:

    nicksalad (10-30-2009)

Bookmarks

Posting Permissions

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