如何在Javascript中每天设置一次变量

问题描述 投票:0回答:3

Coniser变量list = ["A", "B",...]作为字符串列表。我想使用一个Javascript程序,每天从该列表中选择三个字符串并将其写入HTML字段。

目前我用

function getRandom(arr, n) {
var result = new Array(n),
    len = arr.length,
    taken = new Array(len);
if (n > len)
    throw new RangeError("getRandom: more elements taken than available");
while (n--) {
    var x = Math.floor(Math.random() * len);
    result[n] = arr[x in taken ? taken[x] : x];
    taken[x] = --len in taken ? taken[len] : len;
}
return result;
}

smallList = getRandom(list, 3); 

var htmlTags = [
"tag1",
"tag2",
"tag3"
];
for (var i = 0; i < htmlTags.length; i++) {
    document.getElementById(htmlTags[i]).innerHTML = smallList[i];
}

现在,每次刷新网站时,此列表都会获得新条目。是否有一种方法,smallList只能每天/小时/分钟/只在使用javascript的时间段设置一次?

javascript html
3个回答
1
投票

所以你想:

  • 从列表中选择三个值并在网页上显示
  • 每天,选择三个新值来显示一整天
  • 无论客户端如何,访问该页面的每个人都应该看到相同的值

正如其他人所建议的那样,对于服务器端任务而言,它比客户端更好。

例如,您可能有一个服务器页面,用于检查存储在缓存中的值是否存在。缓存将设置为24小时。如果缓存不可用,则会创建一个新的缓存对象,其半衰期为24小时。在缓存中,您还可以存储要检索的值。

然后,您可以检索缓存并输出值。缓存的具体实现取决于您的服务器端语言。


0
投票

好的:通过存储的价值观(COOKIE,SESSION,LOCAL STORAGE,MEMORY):

对于每个用户,您必须在浏览器中使用cookie,会话或写入本地存储。

对于所有用户,您必须在某个地方使用服务器变量,如数据库,文件或内存。

您将值设置为在一天中过期,并在过期时重新生成。这是您从大多数人那里得到的答案,因为这是他们知道如何解决这个问题的唯一方法,即从所有地点轮询的单一设定值。

更好:通过确定性伪随机数发生器播种日期:

或者如果你真的雄心勃勃并且不想依赖于你在某处设置的值,你可以使用或编写一个:确定性的伪随机数生成器,它可以从日期开始。由于确定性伪随机发生器从同一种子产生可重复的“randoms”,因此播种日期每天为您提供一个独特的种子,因此每天都有一个独特的随机种子。

function RC4(seed) {
	this.s = new Array(256);
	this.i = 0;
	this.j = 0;
	for (var i = 0; i < 256; i++) {
		this.s[i] = i;
	}
	if (seed) {
		this.mix(seed);
	}
};
RC4.getStringBytes = function(string) {
	var output = [];
	for (var i = 0; i < string.length; i++) {
		var c = string.charCodeAt(i);
		var bytes = [];
		do {
			bytes.push(c & 0xFF);
			c = c >> 8;
		} while (c > 0);
		output = output.concat(bytes.reverse());
	}
	return output;
};
RC4.prototype._swap = function(i, j) {
	var tmp = this.s[i];
	this.s[i] = this.s[j];
	this.s[j] = tmp;
};
RC4.prototype.mix = function(seed) {
	var input = RC4.getStringBytes(seed);
	var j = 0;
	for (var i = 0; i < this.s.length; i++) {
		j += this.s[i] + input[i % input.length];
		j %= 256;
		this._swap(i, j);
	}
};
RC4.prototype.next = function() {
	this.i = (this.i + 1) % 256;
	this.j = (this.j + this.s[this.i]) % 256;
	this._swap(this.i, this.j);
	return this.s[(this.s[this.i] + this.s[this.j]) % 256];
};
function RNG(seed) {
	if (seed == null) {
		seed = '' + Math.random() + Date.now();
	} else if (typeof seed === "function") {
		// Use it as a uniform number generator
		this.uniform = seed;
		this.nextByte = function() {
			return ~~(this.uniform() * 256);
		};
		seed = null;
	} else if (Object.prototype.toString.call(seed) !== "[object String]") {
		seed = JSON.stringify(seed);
	}
	this._normal = null;
	if (seed) {
		this._state = new RC4(seed);
	} else {
		this._state = null;
	}
}
RNG.prototype.nextByte = function() {
	return this._state.next();
};
RNG.prototype.uniform = function() {
	var BYTES = 7; // 56 bits to make a 53-bit double
	var output = 0;
	for (var i = 0; i < BYTES; i++) {
		output *= 256;
		output += this.nextByte();
	}
	return output / (Math.pow(2, BYTES * 8) - 1);
};
RNG.prototype.random = function(n, m) {
	if (n == null) {
		return this.uniform();
	} else if (m == null) {
		m = n;
		n = 0;
	}
	return n + Math.floor(this.uniform() * (m - n));
};
RNG.$ = new RNG();
Date.prototype.yyyymmdd = function() {
	var mm = this.getMonth() + 1; // getMonth() is zero-based
	var dd = this.getDate();
	return [this.getFullYear(), !mm[1] && '0', mm, !dd[1] && '0', dd].join(''); // padding
};

// Using the Date like so will give you the same random between 40 and 50 for the same day
var rng = new RNG((new Date).yyyymmdd()); rng.random(40, 50);

// Test with dates
var rng = new RNG('20180301'); rng.random(40, 50);
var rng = new RNG('20180302'); rng.random(40, 50);
var rng = new RNG('20180301'); rng.random(40, 50);

0
投票

将列表存储在localStorage或Cookie中。还存储时间戳。

使用setTimeout(function(){...},n)检查时间戳并根据需要更新值。

如果页面刷新或重新加载,则执行检查存储的内容。如果不存在,请创建列表并设置时间戳。如果数据确实存在,则比较时间戳并根据需要更新列表。

如果您需要列表在用户之间保持一致,则需要在服务器端存储,检查和计算所有内容。

localStorage.savedData = {
    timestamp: new Date(),
    dataList: ['a','b','c']
}

要从localStorage获取值:

// you don't have to create variables, you can just use localStorage.[property] to get compare any value
let ts = localStorage.timestamp; // Date object
let dl = localStorage.dataList; // Array of values

有关localStorage的更多信息,请参阅(或搜索网页) - > https://www.w3schools.com/html/html5_webstorage.asp

© www.soinside.com 2019 - 2024. All rights reserved.