<!DOCTYPE html> <html lang="ru"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Stateful app</title> <link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap.css"> </head> <body class="container-fluid p-2 col-12 col-md-8 col-lg-6"> <div class="card text-center mb-2"> <div class="card-title"> Переменная в глобальном контексте </div> <div class="card-body"> <div class="card-text" id="gc-content"></div> <button class="btn btn-primary col-6 mt-2" onclick="updateGcContent(1)">Up</button> </div> </div> <div class="card text-center"> <div class="card-title"> Переменная в хранилище </div> <div class="card-body"> <div class="card-text" id="ls-content"></div> <button class="btn btn-primary col-6 mt-2" onclick="updateLsContent(1)">Up</button> </div> </div> </body> <script> document.addEventListener('DOMContentLoaded', () => { updateGcContent(); updateLsContent(); }); const updateGcContent = (value = 0) => { const newState = (globalThis.myAppState || 0) + value; globalThis.myAppState = newState; document.getElementById('gc-content').innerText = newState; } const updateLsContent = (value = 0) => { const lsKey = 'state'; const newState = parseInt(localStorage.getItem(lsKey) || 0) + value; localStorage.setItem(lsKey, newState); document.getElementById('ls-content').innerText = newState; } </script> </html>