nodejs 學習筆記

學習標準輸入輸出流

根據老外的教程,動手作demo,感受棒棒噠 ^_^學習

//實踐是檢驗真理的惟一標準
var q = ['what\'s your name', 'how old are you', 'what do you like']

var question_proxy = function () {
  var count = 0;

  function question () {
    process.stdout.write(q[count] + '>');
    count++;
  }

  function question_proxy() {
    if (count === q.length) {
      process.exit();
    } else {
      question();
    }
  }

  return question_proxy;
}();

question_proxy();

process.stdin.on('data', function(d) {
  question_proxy();
});

圖片描述

loading百分比demo

var setLoading = function(total, delay) {
  var over = 0;
  var timer = setInterval(function() {
    over += delay;
    process.stdout.cursorTo(0);
    process.stdout.write(`waiting...${Math.floor(over / total * 100)}%`);
    if (over >= total) {
      clearInterval(timer);
      process.exit();
    }
  }, delay);
};

setLoading(3000, 50);

圖片描述

相關文章
相關標籤/搜索