如何捕获服务器错误JS
how to catch the server error JS
我正在学习JavaScript,通过反复试验,有时服务器没有响应,并在内部生成一个Internal Server Error (500)
,随着错误的积累,你开始滞后页面。
也许我assemble 代码写错了?还是我的功能组合不好?
尽管我用 try/catch
捕获了 500 错误,并试图调用函数 update
但我无法捕获他。
有时 JSON 也会出现空值,并发送错误。
捕获错误对我来说合适吗?
这是代码。
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();
}
}
我正在学习JavaScript,通过反复试验,有时服务器没有响应,并在内部生成一个Internal Server Error (500)
,随着错误的积累,你开始滞后页面。
也许我assemble 代码写错了?还是我的功能组合不好?
尽管我用 try/catch
捕获了 500 错误,并试图调用函数 update
但我无法捕获他。
有时 JSON 也会出现空值,并发送错误。 捕获错误对我来说合适吗?
这是代码。
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();
}
}