首页 > 技术文章 > jQuery 定时读取数组中的值

duhaoran 2020-09-24 16:32 原文

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>test</title>
    <script src="/jquery-3.4.1.min.js"></script>
    <script>
        var b = new Array(5000, 6000, 4000);
        $(function () {
            setSelectedItem(0);
            var selectedIndex;
            var playID;
            function setSelectedItem(index) {
                selectedIndex = index;
                clearInterval(playID);
                //自动播放
                playID = setInterval(function () {
                    var index = selectedIndex + 1;
                    
                    if (index > b.length - 1) index = 0;
                    alert(b[index]);
                    setSelectedItem(index);
                }, b[index]);
            }
        })
    </script>
</head>

<body>
</body>
</html>

  上面代码5秒后弹出50006秒后弹出60004秒后弹出4000依次循环弹出。

  后续会陆续更新其他资料,喜欢请关注哦!

  我的博客:https://www.cnblogs.com/duhaoran/

推荐阅读