How to start number on refresh

╄→尐↘猪︶ㄣ 提交于 2019-12-24 06:49:21

问题


This code is for Tamper-monkey and it works amazon. It just changes the look of how much money you have and it counts up, I wanted to know if it is possible to make it so whatever number it is on when you refresh the counter starts at that number.

var oof = document.getElementById("gc-ui-balance-gc-balance-value");
oof.innerHTML = "0";
function animateValue(id){
var obj = document.getElementById(id);
var current = parseInt(obj.innerHTML);

setInterval(function(){
    obj.innerHTML =current++;
},0.1);
}

animateValue('gc-ui-balance-gc-balance-value');

回答1:


You need to save the number in browser's memory, you have two options:

  • local storage
  • cookies

And the initial value would be the stored value or the default value if nothing's stored.

Example with localStorage:

var oof = document.getElementById("gc-ui-balance-gc-balance-value");

var lastCount = localStorage.getItem("lastCount");

oof.innerHTML = lastCount || "0";

function animateValue(id) {
    var obj = document.getElementById(id);
    var current = parseInt(obj.innerHTML);

    setInterval(function () {
        var nextCount = current++;
        localStorage.setItem("lastCount", nextCount);
        obj.innerHTML = nextCount;
    }, 0.1);
}


来源:https://stackoverflow.com/questions/55127520/how-to-start-number-on-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!