我正在为此使用Node.js(和Discord.js,但我认为回答这个问题并不相关)。
我有一段现有的文本(我们称之为baseText
),我想使用下面ex
变量中的数据对它进行编辑(本质上是替换)(这些是相当大的文本,因为它们是从discord.js嵌入的)。
用户第一次发送输入时,将编辑baseText
,使其成为ex1
的值。
然后当用户再次发送输入时,它将编辑baseText
,使其成为ex2
的值。
这将继续通过我的8段文本。
这就是我目前的工作方式:
const ex1 = stuff1
const ex2 = stuff2
//have more variables, but not needed for the example
const editFunc1 = function(){
baseText.edit(ex1) //changes baseText to the value of ex1, but baseText is still what I need to edit later.
}
const editFunc2 = function(){
baseText.edit(ex2) //changes baseText to the value of ex2 - (baseText is still the edit point even though the value of it was overridden with ex1 earlier.
}
//have more functions, but not needed for the example
--------------
//
let number = 1 //this is used as my reset point, so when I want I can change number to be 1 again so when the function is called it starts with ex1 again.
if (number === 1) {
exampleFunc1()
number++
}
if (number === 2) {
exampleFunc2()
number++
}
//have more statements, but not needed for the example
我只是想知道有没有更短的方法来做这件事?因为我不知道如何需要不同的变量(ex1
,ex2
等)来保存我需要的数据。
听起来您想要使用某种队列。通常,您使用一个数组,然后将其拉出并调用您的代码。
null
var values = [1,2,3,4,5,6]
function myQueue (arr) {
const items = arr.slice();
return function () {
return arr.length ? arr.shift() : null;
}
}
var getValues = myQueue(values);
function test() {
var nextValue = getValues();
console.log(nextValue)
}
document.querySelector('button').addEventListener('click', test);
<button>Test</button>