[removed] setTimeout doesn't pause the loop

后端 未结 2 1900
误落风尘
误落风尘 2021-01-27 01:29

I need to make some delays in my loop, every time after some amount of data (after a few cycles/iterations through my loop) is sent to the server.

Sending data and recei

2条回答
  •  悲哀的现实
    2021-01-27 02:07

    You are continuously spawning multiple calls to process() immediately in the while and then telling process to wait 5 seconds before that callback happens.

    // Run this loop over and over again
    while (true) {
        // Create a function called process that process data
        var process = (function () {
            // Do something with data
            console.log("Something");
            // Wait a few seconds and do it again
            setTimeout(process, 5000);
        // This () right here says call process right now
        }());
    }
    

提交回复
热议问题