最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - Why is my synchronous code using setTimeout behaving asynchronous in JavaScript? - Stack Overflow

matteradmin8PV0评论

I'm trying to loop 10x over a test (waiting for the condition to be true), but somehow it's not working.

Here is what I have:

// my counter
$.testHelper.countDown = function(test, iteration) {
    var ticker = iteration || 0;
    if (ticker > 10) {
      return false;
     }
    window.setTimeout(function() {
        if (test === true) {
            console.log("SENDING");
            return true;
        }
        ticker += 1;
        $.testHelper.countDown(test, ticker);
     }, 1000);
    };

    // my test     
    $.testHelper.testForElement = function(element) {
        var result;   
        console.log($i.find(element).length > 0); // true
        result = $.testHelper.countDown(
             $i.find(element).length > 0
        );
        console.log(result);  // undefined
        return result;
     };

My problem is that although my condition equates to true before I'm calling countdown, the answer from countDown is undefined. So my logs e in like this:

// true - before firing my countDown method
// undefined - should not log
// SENDING - response from countDown (= true)

Question:
From the code displayed, is there a reason, why my undefined is logged before countDown is through and returns true?

Thanks!

I'm trying to loop 10x over a test (waiting for the condition to be true), but somehow it's not working.

Here is what I have:

// my counter
$.testHelper.countDown = function(test, iteration) {
    var ticker = iteration || 0;
    if (ticker > 10) {
      return false;
     }
    window.setTimeout(function() {
        if (test === true) {
            console.log("SENDING");
            return true;
        }
        ticker += 1;
        $.testHelper.countDown(test, ticker);
     }, 1000);
    };

    // my test     
    $.testHelper.testForElement = function(element) {
        var result;   
        console.log($i.find(element).length > 0); // true
        result = $.testHelper.countDown(
             $i.find(element).length > 0
        );
        console.log(result);  // undefined
        return result;
     };

My problem is that although my condition equates to true before I'm calling countdown, the answer from countDown is undefined. So my logs e in like this:

// true - before firing my countDown method
// undefined - should not log
// SENDING - response from countDown (= true)

Question:
From the code displayed, is there a reason, why my undefined is logged before countDown is through and returns true?

Thanks!

Share Improve this question asked Mar 28, 2014 at 23:06 frequentfrequent 28.6k61 gold badges187 silver badges336 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Erm, because setTimeout is always asynchronous? That's kind of the whole point.

Here's a possibility for you:

function when(condition,then) {
    // condition must be a callback that returns `true` when the condition is met
    if( condition()) then();
    else setTimeout(function() {when(condition,then);},1000);
}

This will poll once every second until the condition is met, and then do whatever is given in the then callback.

Post a comment

comment list (0)

  1. No comments so far