最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return true and setimout - Stack Overflow

programmeradmin0浏览0评论

Why doesn't this function return true?

function test(str) {
    window.setTimeout(function() {
        if(str == 'ok') {
            return true;
        }
    }, 1000);
}

console.log(test('ok'));

That's not exactly what i want to do. I have a function nammed test() who does some actions after 1 second. I want to execute next function, when test() is finished (so after the timeout).

How i can know when test i finished ?

Why doesn't this function return true?

function test(str) {
    window.setTimeout(function() {
        if(str == 'ok') {
            return true;
        }
    }, 1000);
}

console.log(test('ok'));

That's not exactly what i want to do. I have a function nammed test() who does some actions after 1 second. I want to execute next function, when test() is finished (so after the timeout).

How i can know when test i finished ?

Share Improve this question edited Nov 30, 2012 at 10:29 David Pärsson 6,2573 gold badges39 silver badges52 bronze badges asked Nov 30, 2012 at 10:01 dragon75dragon75 691 silver badge3 bronze badges 4
  • 3 What is the purpose of calling setTimeout like this? – Akhil Sekharan Commented Nov 30, 2012 at 10:03
  • I want to do some actions, and after a time, i want to know when this function is finished – dragon75 Commented Nov 30, 2012 at 10:07
  • You are on the wrong track. Cos set timeout will call the function in a different scope. Can i know if you have any function to call after the setTimeout function is finished and I can help you. – Akhil Sekharan Commented Nov 30, 2012 at 10:09
  • Test is finished long before the setTimeout is executed. To know when something is finished, use callbacks as described in the answers. – David Pärsson Commented Nov 30, 2012 at 10:30
Add a comment  | 

4 Answers 4

Reset to default 9

Tracing your code, here's what happens.

  1. test() is invoked.
  2. setTimeout schedules a function to be called 1000 ms later.
  3. test() concludes execution, no return statement was executed, so undefined is returned instead.
  4. about 1000 ms later the scheduled function fires.
  5. The scheduled function returns true to nothing.

In other words, it just doesn't work that way. The JS interpreter does not pause, it continues over the timeout. You cannot pause execution in JS.


Instead you typically use callbacks:

function test(str, callback) {
  window.setTimeout(function() {
    if (str === 'ok') {
      callback(true);
    }
  }, 1000);
}

// logs 'true' 1000 ms later
test('ok', function(result) {
  console.log(result);
});

// logs nothing, callback never fires
test('NOTOK!', function(result) {
  console.log(result);
});

This code will do more what you seem to have expected.

It does not return true because the setTimeout call is asynchronous. Also, the return value true in your code comes from an inner function.

The normal way of handling such program flow is to pass a callback to the asynchronous function.

function test(str, callback) {
    window.setTimeout(function() {
        callback(str == 'ok');
    }, 1000);
}

test('ok', function (result) {
    console.log(result);
});

The function passed as the second argument to test() will be called when setTimeout executes the code. The argument to the callback function will tell if str is was ok or not.

For starters, settimeout is an asynchronous method, so the actual function test() will have finished and returned before the settimout code runs.

Secondly however, you are only returning true from the settimeout function not the test function, so you will never get anything other than false.

It doesn't return true because asynchronous function setTimeout() will execute after 1000 ms and console.log will execute in normal fashion without waiting of your 'test' function.

发布评论

评论列表(0)

  1. 暂无评论