函数未按指令顺序执行

时间:2014-03-03 15:37:04

标签: javascript jquery callback

我正在研究firefox插件的现有javascript代码库,虽然我通过教程学习了这门语言,但我从未使用过大型代码库。我正在尝试调试由ui事件引起的操作,以下是附加到ui click的回调

  builder.gui.menu.addItem('run', _t('menu_run_locally'), 'run-locally', function() {
    builder.dialogs.runall.runLocally(true);
    alert("for debugging")// my debugging statement
  });

builder.dialogs.runall.runLocally = function(currentScriptOnly) {
  builder.dialogs.runall.currentScriptOnly = currentScriptOnly;
  builder.dialogs.runall.rc = false;
  builder.dialogs.runall.run();
  alert("Another debug statement");
};

对我来说非常奇怪的是,当我点击按钮时,我会在涉及提取页面的实际操作之前获得两个警报。为什么会这样。 JavaScript是否以与我假设不同的方式工作。

我正在开发一个开源项目,以下是它的链接。  [开源代码] https://github.com/sebuilder/se-builder/blob/master/seleniumbuilder/chrome/content/html/js/builder/gui/menu.js#L153

是对我正在处理的上述代码行的引用。

builder.dialogs.runall.run = function() {
  builder.dialogs.runall.hide();
  jQuery('#edit-suite-editing').hide();
  builder.dialogs.runall.requestStop = false;
  builder.dialogs.runall.running = true;

  builder.dialogs.runall.info_p = newNode('p', {id:'infop'}, _t('running_scripts'));

  // Display the scripts in a similar fashion to the steps are shown in the record interface.
  builder.dialogs.runall.scriptlist = newFragment();

  var scriptNames = builder.suite.getScriptNames();
  var scripts = builder.suite.scripts;
  builder.dialogs.runall.getAllRows(scripts, function(scriptIndexToRows) {
    builder.dialogs.runall.runs = [];
    var runIndex = 0;
    for (var i = 0; i < scripts.length; i++) {
      var script = scripts[i];
      if (builder.dialogs.runall.currentScriptOnly && script != builder.getScript()) {
        continue;
      }
      var name = scriptNames[i];
      var rows = scriptIndexToRows[i];
      for (var j = 0; j < rows.length; j++) {
        var run = {
          name: name + (rows.length > 0 ? (" " + _t('row', j)) : ""),
          script: script,
          scriptIndex: i,
          initialVars: rows[j]
        };
        builder.dialogs.runall.runs.push(run);

        var sid = 'run-num-' + runIndex++;
        builder.dialogs.runall.scriptlist.appendChild(
          newNode('div', {id: sid, 'class': 'b-suite-playback-script', 'style': 'padding: 2px; padding-left: 5px; padding-right: 5px; margin-bottom: 1px; border-radius: 5px;'},
            newNode('div',
              makeRunEntry(run),
              makeViewResultLink(sid)
            ),
            newNode('div', {'class':"step-error", id:sid + "-error", style:"display: none"})
          )
        );
      }
    }

    builder.dialogs.runall.stop_b = newNode('a', _t('stop'), {
      'class': 'button',
      click: function () {
        builder.dialogs.runall.stoprun();
      },
      href: '#stop'
    });

    builder.dialogs.runall.close_b = newNode('a', _t('close'), {
      'class': 'button',
      click: function () {
        jQuery(builder.dialogs.runall.dialog).remove();
      },
      href: '#close'
    });

    builder.dialogs.runall.dialog = newNode('div', {'class': 'dialog'});
    jQuery(builder.dialogs.runall.dialog)
      .append(builder.dialogs.runall.info_p)
      .append(builder.dialogs.runall.scriptlist)
      .append(newNode('p',
        newNode('span', {id: 'suite-playback-stop'}, builder.dialogs.runall.stop_b),
        newNode('span', {id: 'suite-playback-close', style: 'display: none;'}, builder.dialogs.runall.close_b)
      ));

    if (builder.dialogs.runall.runs.length > 1) {  
      builder.dialogs.show(builder.dialogs.runall.dialog);
    }

    builder.dialogs.runall.currentRunIndex = -1; // Will get incremented to 0 in runNextRC/Local.
    if (builder.dialogs.runall.rc) {
      builder.dialogs.runall.runNextRC();
    } else {
      builder.dialogs.runall.runNextLocal();
      alert("builder.dialogs.runall.runNextLocal()");
    }
  });
};

builder.dialogs.runall.stoprun = function() {
  builder.dialogs.runall.requestStop = true;
  jQuery('#suite-playback-stop').hide();
  try {
    builder.dialogs.runall.currentPlayback.stopTest();
  } catch (e) {
    // In case we haven't actually started or have already finished, we don't really care if this
    // goes wrong.
  }
  setTimeout(function() {
    builder.dialogs.runall.running = false;
  }, 100);
};

builder.dialogs.runall.processResult = function(result) {
  if (result.url) {
    jQuery("#run-num-" + builder.dialogs.runall.currentRunIndex + "-view").attr('href', result.url).show();
  }
  if (result.success) {
    jQuery("#run-num-" + builder.dialogs.runall.currentRunIndex).css('background-color', '#bfee85');
  } else {
    if (result.errormessage) {
      jQuery("#run-num-" + builder.dialogs.runall.currentRunIndex).css('background-color', '#ff3333');
      jQuery("#run-num-" + builder.dialogs.runall.currentRunIndex + "-error").html(" " + result.errormessage).show();
    } else {
      jQuery("#run-num-" + builder.dialogs.runall.currentRunIndex).css('background-color', '#ffcccc');
    }
  }
};

1 个答案:

答案 0 :(得分:1)

我认为未按顺序执行的操作确实以异步顺序执行。