如何在setTimeout()完成之前停止处理?

时间:2017-05-08 04:35:22

标签: javascript jquery

请假设:

setTimeout(function(){
    console.log('one');
}, 3000);

console.log('two');

我想首先打印one,然后在控制台中打印two。当前代码首先打印two

如何在setTimeout()完成之前停止处理?

注意到,我想执行setTimeout()一次。不是每3秒。

这是我的真实代码:

var arr = ['mohammad', 'ali', 'zahra', 'fatemeh'],
  res = [],
  status = true;

function myfunc() {
  if (status == true) {
    alert('test');
    console.log('Engine started fetching a new one!');
    status = false;
    $('.im_dialogs_search_field').val(arr[0]);
    $('.im_dialogs_search_field').trigger('change');
    setTimeout(function() {
      if ($('li.im_dialog_wrap').length) {
        res.push($(this.find('.im_dialog_photo').attr('src')));
      } else {
        console.log('There is no result for ' + arr[0]);
        status = true;
      }
    }, 3000);
    arr.shift();
  } else {
    console.log('Engine is bussy!');
  }
}

当我调用myfunc()函数时,它总是抛出Engine is bussy!。怎么了?

0 个答案:

没有答案