If, when I try to write, the storage is full, I want to delete the oldest one, so that a place for a new one appears. I understand that the criterion of fullness is not the number of records, but the amount of memory, but I have approximately the same records in terms of volume. In a pinch, you can delete a dozen of the oldest entries. The question is how to explain which records are old. All entries are numbered in order, and deleting entries with numbers from 0 to 10 is not difficult, but this is a special case. If you re-overflow these records will be gone.

  • one
    localStorage.removeItem (localStorage.key (0)); When you delete the numbers will move. This is in the degenerate case, when we don’t care about changing the records in the stack, they will only be added. If it's not all the same, you need to keep the date of change in a hundredfold and write a wrapper on top of it - Duck Learns to Take Cover

1 answer 1

Recently I wrote a case study on localStorage, perhaps you will need some principles of the script http://romanzhivo.com/localstorage-methods-properties-example/#user-visit-notification

Alternatively, enter a conditional separator and record the time and date of the entry made in the store with the key through the separator, and if necessary, delete the time and date. A rough example for comparing two records might be:

function getTimeFunc() { var actualTime = new Date(); if(actualTime.getSeconds() < 10) { var seconds = '0' + actualTime.getSeconds(); } else { var seconds = actualTime.getSeconds(); } if(actualTime.getMinutes() < 10) { var minutes = '0' + actualTime.getMinutes(); } else { var minutes = actualTime.getMinutes(); } if(actualTime.getHours() >= 10 && actualTime.getHours() <= 23) { var hours = actualTime.getHours(); } else { var hours = '0' + actualTime.getHours(); } if(actualTime.getDate() < 10) { var currentDate = '0' + actualTime.getDate(); } else { var currentDate = actualTime.getDate(); } if(actualTime.getMonth() + 1 < 10) { var currenMonth = '0' + (actualTime.getMonth() + 1); } else { var currenMonth = actualTime.getMonth() + 1; } var currentYear = actualTime.getFullYear(); var currentTime = hours + ':' + minutes + ':' + seconds; return { time: currentTime+', '+ currentDate + '.' + currenMonth + '.' + currentYear } } function writeLS1() { var time = getTimeFunc().time; localStorage.someData1 = 123 + '; time ' + time; } function writeLS2() { var time = getTimeFunc().time; localStorage.someData2 = 123 + '; time ' + time; } writeLS2(); function getLSdata() { var data1 = localStorage.someData1; var data2 = localStorage.someData2; var position1 = data1.indexOf('time'); var position2 = data2.indexOf('time'); var time1 = data1.substring(position1+5, 30); var time2 = data2.substring(position2+5, 30); var hours1 = parseInt(time1.substring(0,2)); var minutes1 = parseInt(time1.substring(3,5)); var seconds1 = parseInt(time1.substring(6,8)); var date1 = parseInt(time1.substring(10,12)); var month1 = parseInt(time1.substring(13,15)); var year1 = parseInt(time1.substring(16,20)); var hours2 = parseInt(time2.substring(0,2)); var minutes2 = parseInt(time2.substring(3,5)); var seconds2 = parseInt(time2.substring(6,8)); var date2 = parseInt(time2.substring(10,12)); var month2 = parseInt(time2.substring(13,15)); var year2 = parseInt(time2.substring(16,20)); if( hours1 <= hours2 && minutes1 <= minutes2 && seconds1 <= seconds2 && date1 <= date2 && month1 <= month2 && year1 <= year2 ) { alert('Удалить data1'); } else { alert('Удалить data2'); } } setTimeout(function() { writeLS1(); getLSdata(); }, 2000); 

https://jsfiddle.net/Romanzhivo/mpwxLswe