[jQuery] getScript - Site does not finish loading?

2010-01-12 Thread chricke
hi all,

as i understand the getScript function should be asyncronous, but when
i use it the website won't finish loading (in firefox).

my code:

jQuery(document).ready(function(){
function get_url_param( name )
{
name = name.replace(/[\[]/,\\\[).replace(/[\]]/,\\\]);

var regexS = [\\?]+name+=([^#]*);
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );

if ( results == null )
return ;
else
return results[1];
}

function getCounter(){
jQuery.getScript('http://example.com/counter?id='+get_url_param
('id'), function(){
jQuery('#counterfu_online_count').text(count);
getCounter();
});
}

getCounter();
});

getCounter() calls itself so the counter this script is used for is
updated - all works well, just the page keeps loading...

so whats the problem here? why does the website not finish loading?


Re: [jQuery] getScript - Site does not finish loading?

2010-01-12 Thread Nathan Klatt
On Tue, Jan 12, 2010 at 3:53 AM, chricke c.beckm...@vectan.de wrote:
 as i understand the getScript function should be asyncronous, but when
 i use it the website won't finish loading (in firefox).
snip
        function getCounter(){
                jQuery.getScript('http://example.com/counter?id='+get_url_param
 ('id'), function(){
                        jQuery('#counterfu_online_count').text(count);
                        getCounter();
                });
        }
        getCounter();
 });

 getCounter() calls itself so the counter this script is used for is
 updated - all works well, just the page keeps loading...

 so whats the problem here? why does the website not finish loading?

Okay, you acknowledge you've got a recursive function there (sorta -
is there a special term for recursion through a callback like that?
Anyone?) but do you admit to infinite recursion? If so, could you
point us to a page that causes the problem? Or expound on what you
mean by the page keeps loading? 'Cause, yeah, it's gonna do that as
written. :) You need an exit case for the recursion, possibly
something like the below, though it's hard to comment when I'm not
sure what the reason is to get the same script two times.

var getCounterCount = 0;
function getCounter() {
++getCounterCount;
jQuery.getScript(
'http://example.com/counter?id='+get_url_param('id'),
function() {
jQuery('#counterfu_online_count').text(count);
if (getCounterCount  2) getCounter();
});
}

Nathan