P
P
Pavel Chuev2016-10-18 20:15:47
PHP
Pavel Chuev, 2016-10-18 20:15:47

Instant loading of information from json. How to implement?

I am writing a website for the student radio of my university and came across some difficulty with the implementation of displaying information about the current track.
In icecast, I implemented the output of mount information in json (it looks like this):

{
"response":
      { 
      "name" : "Music",
      "listeners" : "12",
      "description" : "Description",
      "title" : "Soare - all i want",
      "genre" : "Ofther",
      "url" : "example.com"
}
}

By means of json_decode in php I take out the name of the track:
$status = file_get_contents("http://example.com:8000/info.xsl");
$status = json_decode($status);
$track = $status->response;
  
if ($track->title != null)
{
  echo '<marquee behavior="scroll" scrollamount="3" direction="left">'.$track->title.'</marquee>';
  } else {
  echo 'Error:';
}

(I made it a running line right away for convenience)
And with this, perhaps terrible, code, I update the information on the site and throw it into a div block.
function show(url,block) 
  {
    $.ajax({ 
      url: url, 
      cache: false, 
      success: function(html){ 
        block.html(html); 
        } 
      }); 	
  }
      $(document).ready(function(){ 
      setInterval(function() { show('getTrack.php',$('#song-title')); } ,5000);
      });

But because of the interval of 5 seconds, the track name is not loaded immediately and spoils the view with an empty line. How can I implement instant track loading? (an interval of 1 second will only burn listener traffic in vain). I'm waiting for help)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-10-18
@AllDecay

$(document).ready(function(){ 
      show('getTrack.php',$('#song-title'));
      setInterval(function() { show('getTrack.php',$('#song-title')); } ,5000);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question