setTimeout inside while loop

前端 未结 3 1492
故里飘歌
故里飘歌 2020-12-18 04:41

I\'ve searched for how to use setTimeOut with for loops, but there isn\'t a lot on how to use it with while loops, and I don\'t see why there should be much dif

3条回答
  •  庸人自扰
    2020-12-18 05:15

    The while loop is creating trouble, like jrdn is pointing out. Perhaps you can combine both the setInterval and setTimeout and once the src is filled, clear the interval. I placed some sample code here to help, but am not sure if it completely fits your goal:

        var src = '';
        var intervalId = window.setInterval(
            function () {
    
                if (src == '') {
                    setTimeout(function () {
                        //src = $('#currentImage').val();
                        //$("#img_" + imgIdx).attr('src', src);
                        src = 'filled';
                        console.log('Changing source...');
                        clearInterval(intervalId);
                    }, 500);
                }
                console.log('on interval...');
            }, 100);
    
        console.log('stopped checking.');
    

    Hope this helps.

提交回复
热议问题