jQuery/AJAX Refresh?

Status
Not open for further replies.

cxdy

PHP Developer
Jun 8, 2014
100
27
Hey, so I'm working on this script.
The data changes every 3-5 minutes, and I'm not sure how to automatically update it.
The only workaround I have right now is to run it in an iframe, and have the page refresh every 10 seconds. I don't really like that approach.

Is there a way using AJAX or jQuery that I can reload just the php script every few seconds?

What I've tried:
Code:
function refresh(){
    $('#lastfm').load('index.php');
}
setInterval( "refresh()", 10000 );

Along with different variations of that.

The script that I'm trying to refresh can be found here:

Anyone have any ideas? Thank you. :)
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,926
3,921
Something like this should work:
Code:
var scrobblinRefreshTimer = 10;
var scrobblinFileLocation = 'path/to/scrobblin.php';

$(document).ready(function() {
   
  refreshScrobblin();
   
  setInterval(function() { refreshScrobblin() }, scrobblinRefreshTimer * 1000);
   
});

function refreshScrobblin() {
   
    $('.scrobblin-info').load(scrobblinFileLocation);
   
}
 

cxdy

PHP Developer
Jun 8, 2014
100
27
Something like this should work:
Code:
var scrobblinRefreshTimer = 10;
var scrobblinFileLocation = 'path/to/scrobblin.php';

$(document).ready(function() {
  
  refreshScrobblin();
  
  setInterval(function() { refreshScrobblin() }, scrobblinRefreshTimer * 1000);
  
});

function refreshScrobblin() {
  
    $('.scrobblin-info').load(scrobblinFileLocation);
  
}
I would have to have separate files? Like index.php and then scrobblin.php? Because I have it all running in index.php
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,926
3,921
I would have to have separate files? Like index.php and then scrobblin.php? Because I have it all running in index.php
It'd probably be best that it was a separate file, as there's no need to reload the main page every time you want to make an update request. The script will also insert the data initially, so "scrobblin.php" doesn't need to be implemented into "index.php". You could however make a request to "index.php", and get info from the specific div, but I wouldn't recommend that method over the other.
 

cxdy

PHP Developer
Jun 8, 2014
100
27
It'd probably be best that it was a separate file, as there's no need to reload the main page every time you want to make an update request. The script will also insert the data initially, so "scrobblin.php" doesn't need to be implemented into "index.php". You could however make a request to "index.php", and get info from the specific div, but I wouldn't recommend that method over the other.
I was just about to ask if it loaded it automatically. I'm not a Javascript Developer haha.

Thanks a ton, trying this out now!

Edit: It works. Thanks Josh :)
 
Status
Not open for further replies.

Users who are viewing this thread

Top