Created
June 10, 2015 11:35
-
-
Save fwon/7ec1aff809fc7453d9c1 to your computer and use it in GitHub Desktop.
basket.js 笔记
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//每次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