I am developing a Twitter web app for myself. I am retrieving the latest trending topics.
Here’s how I’m doing it:
$.ajax({
url: 'http://api.twitter.com/1/trends/1.json',
dataType: 'jsonp',
success: function(data){
$.each(data[0].trends, function(i){
$('div#trending').hide().append("<p><a href='"+data[0].trends[i].url+"'>"+data[0].trends[i].name+"</a></p>").fadeIn(1000);
//Cache into LocalStorage
localStorage["trending"+i] = data[0].trends[i].name; //Name
localStorage["trendurl"+i] = data[0].trends[i].url;//URL
});
}
});
But sometimes while I am developing it, the rate limit is exceeded.
How can I detect if the rate limit has been exceeded?
I cannot seem to detect if this error is being shown:
{"error":"Rate limit exceeded. Clients may not make more than 150 requests per hour.","request":"\/1\/trends\/1.json"}
I have tried it by:
success: function(data){
if(data[0].error != 'undefined'){
//load localstorage cache
}
}
But that doesn’t seem to work.
Please help.
Thanks 🙂
The Twitter API sends a HTTP 400 status code when you are rate limited, so check for that:
Also note that your comparison is a bit wrong.
data[0].error != 'undefined'will always yield true when the error text is not'undefined'. So even when you are rate limited, the error text won’t be'undefined'and as such succeed. What you probably want to check is this: