I have a javascript code that gives a link to a page, if the page does not answer it gives error and try again. I want to receive the error display a message on the screen
I found some questions about it here but could not apply them in my code. This is my code where I can insert the code so that if there is error, show an image on the screen using document.write
window.setInterval(function() {
requestURL = "https://api.spark.io/v1/devices/" + deviceID1 + "/" + getFunc + "/?access_token=" + accessToken;
$.getJSON(requestURL, function(json) {
var vdadosdospark=json.result;//coloca resultado do json na variavel
var vdadosdospark=vdadosdospark.replace("-", '')//exclui caractere -
var resultadoA = vdadosdospark.substr(9, 6);//seleciona os caracteres referentes a amperagem
var resultadoB = vdadosdospark.substr(24, 1);
if (resultadoB==1){
document.write("<IMG ALIGN='center' "+
"style='position:absolute; left: 400; top: 100' " +
"SRC='http://www.uairobotics.com/tomada/Images/farol.png'> " +
"<BR><BR>")
}else
{
document.write("<IMG ALIGN='center' "+
"style='position:absolute; left: 400; top: 100' " +
"SRC='http://www.uairobotics.com/tomada/Images/f.png'> " +
"<BR><BR>");
}
});
}, 5000);
use the .done()
/.fail()
methods of
jQuery.getJSON().
//first call
var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
$.getJSON(flickerAPI, {
tags: "mount rainier",
tagmode: "any",
format: "json"
})
.done(function(data) {
$('#log').append('got data in first call<br>');
})
.fail(function(jqxhr, textStatus, error) {
$('#log').append('got error in first call<br>');
});
//second call
$.getJSON("willFail.js", {
name: "John",
time: "2pm"
})
.done(function(json) {
$('#log').append('got data in second call<br>');
})
.fail(function(jqxhr, textStatus, error) {
$('#log').append('got error in second call<br>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="log"></p>