每n秒设置一次Javascript变量。当它改变时也创建一个变量的json数组? [英] Set Javascript variable every n sec. When it's changed also create a json array of variable?

查看:85
本文介绍了每n秒设置一次Javascript变量。当它改变时也创建一个变量的json数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数字变量每隔一秒更新一次mathrandom,像一些数字,比如14323121我也想每秒保存一个数组,比如最新的10个左右。

My variable of numbers updates every one second with mathrandom to some numbers like "14323121" I want to also every seccond save these to an array for say the latest 10 or so.

function EveryOneSec() {
    var numbers = math.Random(); // I want to create an array from this...
setTimeOut(EveryOneSec, 1000);
};

将数字绑定到一个数组:(引用上面的var数字)我想创建一个数组如下所示为最后10次更新?

To bind "numbers" to an array: (Referencing to var numbers above) I want to create an array like below for say the 10 last updates?

{"numbers":["2021030213","32454253"]} // For Reigel..
//doesnt have to be key value pairs. just need the value in legit json.


推荐答案

您的方法出了什么问题?只需在重复函数之外设置变量,然后 JSON对其进行编码

What is wrong with the your approach? Just setup your variables outside of the function that is repeated and then JSON encode it.

var numbers = [];
var jsonString = "";

function EveryOneSec() {
  numbers.push(Math.random());
  jsonString = JSON.stringify({'numbers': numbers});
  setTimeout(EveryOneSec, 1000);
}

但是,我认为您的任务可能是使用自定义事件!您标记了您的问题jQuery,因此您应该查看其 bind()方法。当然,关于这个主题有很多教程

However, I think your task might be an excellent opportunity to use a custom event! You tagged your question jQuery, so you should check out its bind() method. Of course there are many tutorials on the topic.

var numbers = [];
var aPlaceholder = document.createElement("div");
aPlaceholder.bind("arrayUpdatedEvent", {'numbers': numbers}, arrayUpdatedHandler);

function arrayUpdatedHandler(event) {
  var jsonString = JSON.stringify(event.data);
  // do something with your JSON
}

function EveryOneSec() {
  // add number to array
  numbers.push(Math.random());
  // trigger custom event
  aPlaceholder.trigger("arrayUpdatedEvent");
}

// call every 1 second, store in var to be able to cancel it later
var myInterval = setInterval(EveryOneSec, 1000);

正如您所看到的,这里有更多的代码。但是,引入自定义事件会为您提供强大的功能,因为现在您已将JSON创建的变量更新解耦。此外,您可以添加额外的侦听器,也许还可以记录更新的数组,并在数组更新和JSON创建之间插入其他步骤。例如, EveryOneSec()可以触发不同的事件, verifyArrayEvent ,并且只有在数组验证后才触发 arrayUpdatedEvent

As you can see, there's a lot more code here. However, introducing a custom event gives you a lot of power, as now you have decoupled the the variable updating from the JSON creation. Furthermore, you can add additional listeners, perhaps to also log the updated array, and to insert additional steps between array update and JSON creation. For instance, EveryOneSec() could trigger a different event, verifyArrayEvent, and only if the array validates does it trigger arrayUpdatedEvent.

更新:从评论到问题看起来像Ozaki isn'清除他们可以访问其定时器功能中以前定义的变量。通过在函数之外定义一个变量,然后在函数中引用它而不使用var ,它会创建一个新的局部变量,您将创建一个 closure 在这里你可以对函数内部的变量进行操作。正如你可能注意到的,我在这两个例子中都使用了这种技术。

Update: From the comments to the question it looks like Ozaki isn't clear that they can access a previously defined variable within their timer function. By defining a variable outside of a function and then referencing it inside of the function without using var, which would create a new, local variable, you are creating a closure where what you do to the variable inside of the function will be available outside of it. As you probably noticed, I used this technique in both my examples.

希望有帮助。

这篇关于每n秒设置一次Javascript变量。当它改变时也创建一个变量的json数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆