Skip to content

Instantly share code, notes, and snippets.

@d1y
Created April 20, 2020 01:46
Show Gist options
  • Select an option

  • Save d1y/7f2e44e877973646c9fa95affae80aa8 to your computer and use it in GitHub Desktop.

Select an option

Save d1y/7f2e44e877973646c9fa95affae80aa8 to your computer and use it in GitHub Desktop.
检测 localStorage 最大写入值
(function() {
//判断浏览器是否支持localStorage
if(!window.localStorage) {
console.log('当前浏览器不支持localStorage!');
return false;
}
var test = '0123456789';
//递归 加到10kb
var add = function(num) {
num += num;
if(num.length == 10240) {
test = num;
return;
}
add(num);
}
add(test);
var sum = test;
var show = setInterval(function(){
//每次增加10kb
sum += test;
try {
window.localStorage.removeItem('test');
window.localStorage.setItem('test', sum);
//控制台输出当前大小(kb)
console.log(sum.length / 1024 + 'KB');
} catch(e) {
//如果大小超出范围setItem会报异常 这时就是超出最大限制
console.log(sum.length / 1024 + 'KB超出最大限制');
//清除定时
clearInterval(show);
}
}, 0.1)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment