如何使用值的 setInterval 来控制数字数组的每个值作为秒 Reactjs
How to console each value of number array with setInterval of the value as seconds Reactjs
让 listofRandom = [ ]
以一定的时间间隔控制我推入该数组的每个值
例如:让 listofRandom = [5, 1, 7 ]
这里的第一个值为 5,这意味着它必须每 5 秒控制一次“5”,下一个值为 1 必须每 1 秒控制一次“1”,然后每 7 秒控制一次“7”秒。
let listofRandom = [];
let generatedValues = '';
const addValue = () => {
let value = Math.floor(Math.random() * 9) + 1;
listofRandom.push(value);
console.log(listofRandom)
};
listofRandom.map((num) => {
return setInterval(function () {
generatedValue = generatedValue + num.toString();
console.log(generatedValue);
}, num * 1000);
});
return(
<button onclick="addValue()">Add Value To Array </button>)
let listofRandom = [5, 1, 7];
listofRandom.map((num) => {
setInterval(function() {
console.log(num);
}, num * 1000)});
const [values, setValues] = useState([1, 5, 7]);
const [count, setCount] = useState(0);
useEffect(() => {
let counter = count;
const interval = setInterval(() => {
if (counter >= values.length) {
clearInterval(interval);
} else {
console.log(values[counter]);
setCount((count) => count + 1);
counter++;
}
}, values[counter] * 1000);
return () => clearInterval(interval);
}, [count]);
让 listofRandom = [ ]
以一定的时间间隔控制我推入该数组的每个值
例如:让 listofRandom = [5, 1, 7 ]
这里的第一个值为 5,这意味着它必须每 5 秒控制一次“5”,下一个值为 1 必须每 1 秒控制一次“1”,然后每 7 秒控制一次“7”秒。
let listofRandom = [];
let generatedValues = '';
const addValue = () => {
let value = Math.floor(Math.random() * 9) + 1;
listofRandom.push(value);
console.log(listofRandom)
};
listofRandom.map((num) => {
return setInterval(function () {
generatedValue = generatedValue + num.toString();
console.log(generatedValue);
}, num * 1000);
});
return(
<button onclick="addValue()">Add Value To Array </button>)
let listofRandom = [5, 1, 7];
listofRandom.map((num) => {
setInterval(function() {
console.log(num);
}, num * 1000)});
const [values, setValues] = useState([1, 5, 7]);
const [count, setCount] = useState(0);
useEffect(() => {
let counter = count;
const interval = setInterval(() => {
if (counter >= values.length) {
clearInterval(interval);
} else {
console.log(values[counter]);
setCount((count) => count + 1);
counter++;
}
}, values[counter] * 1000);
return () => clearInterval(interval);
}, [count]);