如何自动在随机时间自动显示随机文本然后循环

时间:2019-04-06 15:45:54

标签: javascript

因此,我试图在随机时间后自动添加随机弹出文本,然后循环播放。

可以删除或添加产品

<p class="custom-notification-content">
            Someone from 
   <script>
    var Country = ['Edmonton, Canada', 'California, United States', 'New Jersey, United States','Birmingham, United Kingdom', 'Melbourne, Australia', 'Paris, France'];
    var randCountry = Country[Math.floor(Math.random() * Country.length)];
    document.write(randCountry);</script> 
   </script>
             <br>Purchased <b>

            <script>var randNumber = Math.floor(Math.random() * 9) + 1; document.write(randNumber);</script><script>var Zeros = ['0', '00', '000'];
var randZeros = Zeros[Math.floor(Math.random() * Zeros.length)];
document.write(randZeros);</script></b> 

<script>var Product = ['apple','grapes','comb','hat'];
var randProduct = Product[Math.floor(Math.random() * Product.length)];
document.write(randProduct);</script>   

            <small>(<script>var randNumber = Math.floor(Math.random() * 5) + 2; document.write(randNumber);</script> hours ago)</small>
          </p>

这里发生的是,在随机弹出时间之后,脚本不再具有随机的国家/地区,编号和产品。我需要重新加载网页才能更改结果,我希望文本自动显示

我希望在随机弹出时间之后,从“国家/地区”某人那里购买“随机数”“产品”“时间”。

1 个答案:

答案 0 :(得分:0)

查看以下代码。

<!DOCTYPE html>
<html>
    <head>
        <title>Rand Time Content change</title>
    </head>
    <body>
    <p class="custom-notification-content">
        Someone from <span id="rand-country"></span><br>Purchased <b>
        <span id="rand-number"></span><span id="rand-zeros"></span></b> <span id="rand-product"></span> <small>(<span id="rand-hour"></span> hours ago)</small>
    </p>
    <script type="text/javascript">
        function randContent() {
            var min = 1, // this is the min second
            max = 10; // this is the max second
            var rand = Math.floor(Math.random() * (max - min + 1) + min); //Generate Random number between 1 - 10

            var Country = ['Edmonton, Canada', 'California, United States', 'New Jersey, United States','Birmingham, United Kingdom', 'Melbourne, Australia', 'Paris, France'];
            var randCountry = Country[Math.floor(Math.random() * Country.length)];
            document.getElementById("rand-country").innerHTML= randCountry;

            var randNumber = Math.floor(Math.random() * 9) + 1; 
            document.getElementById("rand-number").innerHTML= randNumber;

            var Zeros = ['0', '00', '000'];
            var randZeros = Zeros[Math.floor(Math.random() * Zeros.length)];
            document.getElementById("rand-zeros").innerHTML= randZeros;

            var Product = ['apple','grapes','comb','hat'];
            var randProduct = Product[Math.floor(Math.random() * Product.length)];
            document.getElementById("rand-product").innerHTML= randProduct;

            var randHour = Math.floor(Math.random() * 5) + 2; 
            document.getElementById("rand-hour").innerHTML= randHour;


            setTimeout(randContent, rand * 1000);
        }

        randContent();  
    </script>
</body>
</html>