jQuery Odometer Count Up – Combine with localstorage to retain number [closed]

I am using jQuery Odometer to show a number that counts upwards each second, starting at # 12508 (see code snippet #one below). Once the page loads the number increments until the user navigates away. When the user returns, it starts again from the set number # 12508.

However, I would like to retain the previously reached number using localstorage, as in the example #two below.

I need some help to combine these snippets so that it loads the Odometer styles and starting number, but retains the local storage on reload.

Here’s what I have so far:

Example #1 the Odometer code:

https://codepen.io/SolaceBeforeDawn/pen/KKOKeXL

jQuery(document).ready(function($) {
        window.odometerOptions = {
          format: '(,ddd).dd'
        };
  // var finalDigit 
  var finalDigit = 12508; // starting number
  setTimeout(function(){
      $('.odometer').html(finalDigit);
    }, 2000);
  setInterval(function(){
    finalDigit += 01;
      $('.odometer').html(finalDigit);
  }, 1000);  // increment of 1 second
        
});
.odometer{
  margin: 0 auto;
  text-align: center;
  font-size:2em;
  border: 1px solid black;
  border-radius: 6px;
  padding: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://github.hubspot.com/odometer/themes/odometer-theme-default.css" />
<script src="https://unpkg.com/[email protected]/odometer.min.js"></script>

<div class="odometer">0,000</div>

Example # 2 Local storage concept:

https://codepen.io/SolaceBeforeDawn/pen/NWQPGMG

function isLocalStorage() {
    try {
        return 'localStorage' in window && window['localStorage'] !== null;
    } catch(e) {
        return false;
    }
}

function setCounter(key, val) {
    localStorage.setItem(key, val);
}

function getCounter(key) {
    return localStorage.getItem(key);
}

(function() {
    
    var key = "myCounter";
    var counter = isLocalStorage() && getCounter(key) || 12508;
    var $placeholder = $(".count");
    $placeholder.html(counter);

    setInterval(function () {
        counter++;
        $placeholder.html(counter);
        isLocalStorage() && setCounter(key, counter);
    }, 1000);
}());
.count{
  font-size:18px;
  color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div class="count"></div>

2

OK never mind, I figured it out and am answering my own question and posting this here for anyone who is looking for something similar

Note, the snippet in SO below appears broken due to Error “Uncaught SecurityError: Failed to read the ‘localStorage’ property from ‘Window’:”

However, the codepen works fine without issue.

https://codepen.io/SolaceBeforeDawn/pen/yLmyYQb

jQuery(document).ready(function ($) {
  window.odometerOptions = {
    format: "(,ddd).dd"
  };
  // settings

  var counter = 12508;
  if (typeof localStorage.getItem("counts") != "object") {
    counter = parseInt(localStorage.getItem("counts"));
  }
  setInterval(function () {
    $(".odometer").html(counter);
    ++counter;
    localStorage.setItem("counts", counter);
  }, 1000);
});
.odometer {
  margin: 0 auto;
  text-align: center;
  font-size: 2em;
  border: 1px solid black;
  border-radius: 6px;
  padding: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://unpkg.com/[email protected]/odometer.min.js"></script>
<link rel="stylesheet" href="https://github.hubspot.com/odometer/themes/odometer-theme-default.css" />

<div class="odometer">0,000</div>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật