如何停止执行javascript?

时间:2018-03-08 13:48:43

标签: javascript

如何在执行后停止执行此Javascript? 通常这个问题是由于函数中缺少return语句引起的。好吧,这两个函数都返回了一些那么为什么Skript继续运行,我能做些什么呢?

var system = require('system');
var args = system.args;

function hexToRgb(hex) {
  // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  hex = hex.replace(shorthandRegex, function(m, r, g, b) {
    return r + r + g + g + b + b;
  });
  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16)
  } : null;
}


function rgb2hex(red, green, blue) {
  var rgb = blue | (green << 8) | (red << 16);
  return '#' + (0x1000000 + rgb).toString(16).slice(1)
}



hexPrimaryColor = args[1];
redPrimaryColor = hexToRgb(hexPrimaryColor).r;
greenPrimaryColor = hexToRgb(hexPrimaryColor).g;
bluePrimaryColor = hexToRgb(hexPrimaryColor).b;
redComplimentaryColor = 255 - redPrimaryColor;
greenComplimentaryColor = 255 - greenPrimaryColor;
blueComplimentaryColor = 255 - bluePrimaryColor;
hexCcomplimentaryColor = rgb2hex(redComplimentaryColor, greenComplimentaryColor, blueComplimentaryColor);
//console.log("Primärfarbe:" + hexPrimaryColor +"\n");
console.log(hexCcomplimentaryColor);
//throw Error();

3 个答案:

答案 0 :(得分:2)

尝试将process.exit(0)添加到程序的末尾

答案 1 :(得分:0)

将代码放入函数中并在返回某处后调用它

condition()
 return; // put your code after return 

callFunction();

function callFunction(){
 hexPrimaryColor = args[1];
 redPrimaryColor = hexToRgb(hexPrimaryColor).r;
 greenPrimaryColor = hexToRgb(hexPrimaryColor).g;
 bluePrimaryColor = hexToRgb(hexPrimaryColor).b;
 redComplimentaryColor = 255 - redPrimaryColor;
 greenComplimentaryColor = 255 - greenPrimaryColor;
 blueComplimentaryColor = 255 - bluePrimaryColor;
 hexCcomplimentaryColor = rgb2hex(redComplimentaryColor, 
 greenComplimentaryColor, blueComplimentaryColor);
 //console.log("Primärfarbe:" + hexPrimaryColor +"\n");
 console.log(hexCcomplimentaryColor);
   //throw Error();
}

答案 2 :(得分:0)

感谢您的回答。实际上代码完全没问题。我刚用phantomjs执行代码,并没有把phantom.exit()放到最后。这就是为什么这个过程没有停止。