onerror事件显示图片 - javascript

时间:2015-03-11 09:04:01

标签: javascript onerror

我有一个javascript代码,它提供了一个页面的链接,如果页面没有回答它会给出错误并再试一次。我想收到错误,在屏幕上显示一条消息

我在这里发现了一些关于它的问题,但无法在我的代码中应用它们。这是我的代码,我可以在其中插入代码,以便在出现错误时,使用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);

1 个答案:

答案 0 :(得分:1)

使用.done() / .fail()方法 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>