如何捕获服务器错误 JS
how to catch the server error JS
我正在学习JavaScript,通过反复试验,有时服务器没有响应,并在内部生成Internal Server Error (500)
,随着错误的累积,您开始滞后页面。
也许我以错误的方式组装代码? 还是我做了糟糕的功能组合?
即使我用 try/catch
捕获了 500 错误,并尝试调用函数update
但我无法抓住他。
此外,有时 JSON 以空值显示,并发送错误。我是否适合捕获错误?
这是代码。
jsfiddle.net/rocr/pa0221k6/
var width = $('.ticker-text').width(),
containerwidth = $('.ticker-container').width(),
left = containerwidth;
var timer;
$(document).ready(function(e){
//timer
function Timer(callback, delay) {
var timerId, start, remaining = delay;
this.pause = function() {
window.clearTimeout(timerId);
remaining -= new Date() - start;
};
this.resume = function() {
start = new Date();
window.clearTimeout(timerId);
timerId = window.setTimeout(callback, remaining);
};
this.resume();
}
//show stock quotes
function update() {
var query = "select * from yahoo.finance.quotes where symbol = ";
var symbolo = "'aapl'";
var yql = "http://query.yahooapis.com/v1/public/yql?q=" + escape(query+symbolo) + "&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=?";
var xhr2 = $.ajax({
url: yql,
jsonp: "myCallback",
dataType: 'json',
success: function(data) {
var keys = data.query.results.quote;
$("#a").html(keys.LastTradePriceOnly);
$("#b").html(keys.LastTradePriceOnly);
update();
},
error: function(xhr, ajaxOptions, thrownError) {
//console.log(xhr.status + " , " +thrownError);
//console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
update();
},
statusCode: {
500: function() {
console.log( "Error 500" );
alert( "Error 500" );
update();
}
},
timeout: 3000
});
}
//move div
function tick() {
if(--left < -width){
left = containerwidth;
}
$(".ticker-text").css("margin-left", left + "px");
//setTimeout(tick, 16);
timer = new Timer(tick, 16);
}
tick();
update();
$("#b").hover(function(){
timer.pause();
$("#b").css("background-color", "yellow");
},function(){
timer.resume();
$("#b").css("background-color", "pink");
});
});
如果未处理 ajax 请求,则再次使用 try 来处理 DOM 中的显示。
success: function(data) {
try{
var keys = data.query.results.quote;
var keys = data.query.results.quote;
$("#a").html(keys.LastTradePriceOnly);
$("#b").html(keys.LastTradePriceOnly);
update();
}
}
相关文章:
- Node.js错误“;ReferenceError:全局未定义“;在从0.10.2更新到0.12.2之后
- 最短路径算法js错误
- Modx数据包管理器ext-all.js错误
- Dropzone引导JS错误
- JS错误导致WordPress插件无法工作
- Date.js错误地分析了ISO 8601 UTC日期
- Ember.js错误“;无法读取属性'容器'未定义的“;使用液体火时
- React.js错误;相邻的JSX元素必须被封装在一个封闭标记“中;
- 获取意外的令牌ILLEGAL JS错误
- 使用动画时出现反应JS错误
- IE8 中未知的 JS 错误
- Angular JS错误:仅在IE 10和IE 11中出现[$rootScope:infdig]
- Meteor JS错误”;ReferenceError:X未定义”;
- Node.js错误:参数太多上传批量数据时出错
- CKEditor选项startupMode导致js错误
- 在出现JS错误的页面上运行外部Javascript
- angular js:错误:$没有在http文章中定义
- 使用JS检测所有JS错误
- Firebug不再显示JS错误
- require.js错误(topojson未加载)