Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Jquery question (Need help)

Featured Replies

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

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.

  • 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() :)

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

  • 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

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?

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

  • 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 :)

  • 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

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

  • 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? :)

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)

  • 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

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.