Skip to content

Instantly share code, notes, and snippets.

@fwon
Created June 10, 2015 11:35
Show Gist options
  • Save fwon/7ec1aff809fc7453d9c1 to your computer and use it in GitHub Desktop.
Save fwon/7ec1aff809fc7453d9c1 to your computer and use it in GitHub Desktop.
basket.js 笔记
//每次set的时候,检查localStorage是否溢出,做LRU处理
var addLocalStorage = function( key, storeObj ) {
try {
localStorage.setItem( storagePrefix + key, JSON.stringify( storeObj ) );
return true;
} catch( e ) {
if ( e.name.toUpperCase().indexOf('QUOTA') >= 0 ) {
var item;
var tempScripts = [];
for ( item in localStorage ) {
if ( item.indexOf( storagePrefix ) === 0 ) {
tempScripts.push( JSON.parse( localStorage[ item ] ) );
}
}
if ( tempScripts.length ) {
tempScripts.sort(function( a, b ) {
return a.stamp - b.stamp;
});
basket.remove( tempScripts[ 0 ].key );
return addLocalStorage( key, storeObj );
} else {
// no files to remove. Larger than available quota
return;
}
} else {
// some other error
return;
}
}
};
//请求超时则中断
setTimeout( function () {
if( xhr.readyState < 4 ) {
xhr.abort();
}
}, basket.timeout );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment