我如何循环if else语句?

时间:2017-03-03 19:28:54

标签: javascript arrays node.js loops

我试图通过这个声明给我一个游戏赢家,但我希望每个游戏获胜者能够显示1000毫秒。现在它循环整个事情,每1000毫秒给我一个完整的列表。说出这个的正确方法是什么,以便它每1000毫秒循环并显示每个?

request("https://api.sportradar.us/ncaamb-t3/games/2017/03/02/schedule.json?api_key=*************", 
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var data = JSON.parse(body);
            data.games.forEach((v) => {

                var homeName = (v.home.name);
                var awayName = (v.away.name);
                var homeScore = (v.home_points);
                var awayScore = (v.away_points);

                if (homeScore > awayScore) {
                    time = setInterval(function (time) {
                        console.log('statuses/update', 
                                    { status: homeName.toUpperCase() + " WIN!" },
                                    function (error, data, response) {});
                    }, 1000);
                } else {
                    console.log('statuses/update', 
                                { status: awayName.toUpperCase() + " WIN!" },
                                function (error, data, response) {});
                }
            });
        }
    });

1 个答案:

答案 0 :(得分:0)

对于任何有兴趣的人来说,这就是我最终让它发挥作用的方式,似乎就是这样做的!

request("https://api.sportradar.us/ncaamb-t3/games/2017/03/06/schedule.json?api_key=**************", 
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var data = JSON.parse(body);
            data.games.forEach((v,i) => {
               setTimeout(() => {

                var homeName = (v.home.name);
                var awayName = (v.away.name);
                var homeScore = (v.home_points);
                var awayScore = (v.away_points);

                if (homeScore > awayScore) {
                        console.log('statuses/update', 
                                    { status: homeName.toUpperCase() + " WIN!" },
                                    function (error, data, response) {});

                } else {
                    console.log('statuses/update', 
                                { status: awayName.toUpperCase() + " WIN!" },
                                function (error, data, response) {});

                }
            }, i * 1000 );
            });

        }
    });