November 10, 201015 yr Yea how can i refresh a div in order to show updated content without page refresh, i've been using .load() in jquery but my div has to be inside a while loop and using this does not work well at all when the div that needs to be refreshed is in a while loop. So whats another way to just refresh the div, to show updated content thanks in advance
November 10, 201015 yr If you're trying to execute an AJAX request (i.e. load()) within a loop - it's called a "race condition". What that means is that the next iteration of the loop will begin before the AJAX request completes. There is a plugin for jQuery to queue AJAX requests - but other than that you need to find a way of buffering the requests and holding the loop in place until it's executed.
November 10, 201015 yr Author If you're trying to execute an AJAX request (i.e. load()) within a loop - it's called a "race condition". What that means is that the next iteration of the loop will begin before the AJAX request completes. There is a plugin for jQuery to queue AJAX requests - but other than that you need to find a way of buffering the requests and holding the loop in place until it's executed. Could u give me an example of refreshing a div without using .load()
November 10, 201015 yr You could just use a set timeout in javascript, that will re-apply the timeout in the complete of the ajax request, seems the logical way to me
November 10, 201015 yr Author You could just use a set timeout in javascript, that will re-apply the timeout in the complete of the ajax request, seems the logical way to me Can u give me an example of how to do this thanks in advance
November 11, 201015 yr Could u give me an example of refreshing a div without using .load() ueu That's not what I said, it's fine to use load, you just need to use a queuing system to stack the AJAX calls, if you're executing them in a loop. http://plugins.jquery.com/project/ajaxqueue Or are you just refreshing the page at a given interval?
November 11, 201015 yr I should really have put the code here too.. <script type="text/javascript"><!-- var timer = false; function load_div() { $('#content').load('http://www.example.com/file.php', function(){ timer = setTimeout('load_div()', 10000); }); } $(function(){ timer = setTimeout('load_div()', 3000); }); --></script> I think a plugin to queue it is a bit overkill tbh, the above won't start the next load timer until a load is completed
November 11, 201015 yr Author I should really have put the code here too.. <script type="text/javascript"><!-- var timer = false; function load_div() { $('#content').load('http://www.example.com/file.php', function(){ timer = setTimeout('load_div()', 10000); }); } $(function(){ timer = setTimeout('load_div()', 3000); }); --></script> I think a plugin to queue it is a bit overkill tbh, the above won't start the next load timer until a load is completed Thank u but 1 question say the div im using to refresh is inside a while loop it tends to duplicate all the post into every post that it refreshes
November 11, 201015 yr Author You've lost me to be honest ok well i use load like this $("#content").load("home.php .contentdiv"); where it says .contentdiv thats the div that load will refresh every so amount of time, well .contentdiv is included inside a while loop insted of it just refreshing the content inside the div it duplicates the content and refreshes it
November 11, 201015 yr You should really be refreshing from some kind of api, not just loading content from a div on a page, that's not really what load is for. If the content on the home.php page is loaded from a database, your best bet would be to create a new page that the load calls, along with the last time the check was made, then query the database based on the last content, loading all the new content
November 11, 201015 yr Author You should really be refreshing from some kind of api, not just loading content from a div on a page, that's not really what load is for. If the content on the home.php page is loaded from a database, your best bet would be to create a new page that the load calls, along with the last time the check was made, then query the database based on the last content, loading all the new content So u mean have the code that querys the database in a seperate file?
November 11, 201015 yr Yeah, if you want to load a post, you don't want to load a whole page of data for that small bit, it's highly inefficient. It's better to use something like $.getJSON to load an object of your data from a php file (use json_encode() in php to format it correctly)
November 11, 201015 yr Author Yeah, if you want to load a post, you don't want to load a whole page of data for that small bit, it's highly inefficient. It's better to use something like $.getJSON to load an object of your data from a php file (use json_encode() in php to format it correctly) Can u give me an example of using that sorry im fairly new to alot of jquery things
November 11, 201015 yr Author lol that's why I linked to the API page with examples Ohhhhh lol i overlooked the link sorry bout that
Create an account or sign in to comment