dimanche 15 mars 2015

Uncaught TypeError: Cannot read property 'listeners' of undefined


Vote count:

0




We've implemented LiveDJ function in IceCast and there is three mount points now, previously there was only one. JSON structure has changed as a result of that. We need to parse this information from active mountpoint: title and listeners number.


I'm trying to determine the active mountpoint using 'if' statement, but I can't find a solution how to avoid this error in browser: "Uncaught TypeError: Cannot read property 'listeners' of undefined". It stops the script.


How could I check if my variable is 'undefined' so I could continue my logic? Note: I get this undefined only when server is started freshly and Live session was not established yet. If I connect and disconnet a live stream, JSON structure changes and I get all variables defined then.





<script>
function radioTitle() {
$.ajax({
url: "http://ift.tt/18sEpsj",
dataType: "text",
success: function(data) {
var json = $.parseJSON(data);

var listeners = json.icestats.source.listeners;
var listenersLive = json.icestats.source[0].listeners; // Error in browser "Uncaught TypeError: Cannot read property 'listeners' of undefined"
var listenersDJ = json.icestats.source[1].listeners;

if(listeners > listenersDJ) {
//Fresh start
}
else if(listenersDJ > listenersLive) {
//autoDJ
}
else {
//Live
}
}
});
}
$(document).ready(function () {
setTimeout(function () {radioTitle();}, 0);
setInterval(function () {radioTitle();}, 15000);
});
</script>




asked 1 min ago







Uncaught TypeError: Cannot read property 'listeners' of undefined

Aucun commentaire:

Enregistrer un commentaire