简体   繁体   English

如何使用自定义延迟在 JavaScript 中生成随机数

[英]How to generate Random number in JavaScript with custom delays

I wrote the code for random number generator in javascript with setTimeout function.我用 setTimeout function 在 javascript 中编写了随机数生成器的代码。 The code is given below.代码如下。

 const [number, setnumber] = useState();
      function randomNumber() {
        let number1 = Math.floor(Math.random() * 21 + 1);
        setnumber(number1);
      }
      setInterval(() => {
        randomNumber();
      }, 6000);

But it keeps randomizing infinitely after few seconds.但它会在几秒钟后无限随机化。

在此处输入图像描述

The requirement is to generate number after the timeout of 20 sec and keep randomize the number for next 4 seconds till the clock is reset to zero.要求是在 20 秒超时后生成数字,并在接下来的 4 秒内保持随机数字,直到时钟重置为零。

and the loop countinous和循环计数

  <CircularProgress
            trackColor="inherit"
            capIsRound
            thickness={'6px'}
            className="circlular progress"
            value={80}
            size={'500px'}
            color={'#20fc94'}
            alignItems={'center'}
            justifyContent={'center'}
            display={'flex'}
          >
            <CircularProgressLabel
              className="circlular progress lable"
              borderRadius={'full'}
              alignItems={'center'}
              justifyContent={'center'}
              display={'flex'}
            >
              <Stack
                border={'5px solid #20FC9D'}
                borderRadius={'full'}
                h={'385px'}
                w={'385px'}
                align={'center'}
                justify={'center'}
              >
                <Stack
                  border={'5px solid #13cee6'}
                  borderRadius={'full'}
                  h={'350px'}
                  w={'350px'}
                  boxShadow={'0 0 25px #13cee6'}
                  align={'center'}
                  justify={'center'}
                >
                  <Stack
                    className="text"
                    h={'full'}
                    w={'full'}
                    p={'12'}
                    align={'center'}
                    justify={'center'}
                  >
                    <Text
                      flex={'2'}
                      h={'fit-content'}
                      w={'fit-content'}
                      fontSize={'1.4em'}
                      background={
                        'radial-gradient(circle, rgba(201,18,191,1) 0%, rgba(134,40,206,1) 51%)'
                      }
                      backgroundClip={'text'}
                      style={{ WebkitTextStroke: '1px #20fc9d' }}
                    >
                      {number}
                    </Text>
                    <Text flex={'2'} color={'#FCA120'} px={'4'} fontSize={'md'}>
                      Will the next number be Higher or Lower
                    </Text>
                  </Stack>
                </Stack>
              </Stack>
            </CircularProgressLabel>
          </CircularProgress>

here is the code for the circular progress and the number generator function call.这是循环进度和数字生成器 function 调用的代码。

Wrap the setInternal inside useEffect hook.将 setInternal 包裹在 useEffect 挂钩中。

   useEffect(()=>{
        setInterval( ()=> setnumber(Math.floor(Math.random * 20 + 1)),
    6000);
    },[]);

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

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