简体   繁体   English

Javascript:如何在不更改代码的情况下更新对象值?

[英]Javascript: How do you update a object value without changing your code?

I have a number in a object value that I want to dynamically update as the code runs. 我有一个对象值中的数字,我希望在代码运行时动态更新。 So for example, if the value of the number is 30, when I run my code, I want to be able to update this number to say.. 20 without me actually having to go into the code and personally update it to 20. 因此,例如,如果数字的值是30,当我运行我的代码时,我希望能够更新这个数字来说.. 20,而我实际上不必进入代码并亲自将其更新为20。

How can I write code that will update this object value? 如何编写将更新此对象值的代码? The trouble I'm having is with a timer system that keeps registering the value of 30 every 1 second instead of the NEW value assigned to it after subtracting some numbers. 我遇到的麻烦是一个定时器系统,它每1秒保持一次30的值,而不是在减去一些数字后分配给它的NEW值。

var count = character.energy; // My Energy level is 30
var counter = setInterval(timer, 1000); 

function timer() { 
  count += characterstats.energyregen;
  if (count >= 30){
     clearInterval(counter);
  }
  document.getElementById("energy").innerHTML = count;
  character.energy = count;
}

Move the declaration and initialization of the count variable inside the timer. 在计时器内移动count变量的声明和初始化。

var counter = setInterval(timer, 1000); 

function timer() { 
  var count = character.energy; // My Energy level is 30
  count += characterstats.energyregen;
  if (count >= 30){
     clearInterval(counter);
  }
  document.getElementById("energy").innerHTML = count;
  character.energy = count;
}

Another way, but may not be what you are looking for , is using local Storage and or cookies to store the value. 另一种方式,但可能不是您正在寻找的,使用本地存储和/或cookie来存储价值。 Though, use judgment on this depending on how important or private the variable information is. 但是,根据变量信息的重要性或私密性,对此进行判断。 I have done this, and encrypted it before storing. 我做了这个,并在存储之前对其进行了加密。 I have stored the value in localStorage, then the object is updated durring load event, to get the new value. 我已将值存储在localStorage中,然后在load事件期间更新对象,以获取新值。 A getter/setter type function is a property on the object, so that it increments what ever you want, everytime the script is ran. getter / setter类型函数是对象的一个​​属性,因此每次运行脚本时它都会增加所需的值。 I do this by storing the object, as a string running JSON on it. 我通过将对象存储为运行JSON的字符串来完成此操作。 If you need it to be updated based on a timed theme, then Mike's answer is a good one. 如果您需要根据定时主题进行更新,那么迈克的回答是好的。 If you were only using a timer, because you were not sure of another method, try out the localStorage option. 如果您只使用计时器,因为您不确定其他方法,请尝试使用localStorage选项。 Has been useful in some instances for me. 在某些情况下对我来说很有用。 Happy coding! 快乐的编码! :) :)

Reference to good post for storing objects in HTML5 localStorage: https://stackoverflow.com/a/2010948/2762516 from a fellow stack user! 参考用于在HTML5 localStorage中存储对象的好帖子:来自其他用户的https://stackoverflow.com/a/2010948/2762516

SORRY! 抱歉! I MISREAD THE QUESTION. 我误解了这个问题。 I thought you wanted to update every time code ran, not while code is running. 我想你想在每次代码运行时更新,而不是在代码运行时更新。 I apologize:) 我道歉:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM