Created
April 20, 2020 01:46
-
-
Save d1y/7f2e44e877973646c9fa95affae80aa8 to your computer and use it in GitHub Desktop.
检测 localStorage 最大写入值
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
| (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