<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <script type="text/javascript"> (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) })() </script> <body> </body> </html>