2017-09-06 12 views
1

私の理解が行く限り、これはstreamであり、常にOracleデータベースに値をストリーミングしています。ノードのタイムアウト機能?

もう一度送信する前に約3秒待つようにタイムアウト機能を実行できるのかどうか疑問に思っています。

var net = require('net'); 
var fs = require('fs'); 
var oracledb = require('oracledb'); 
var dbConfig = require('./dbconfig.js'); 


var client = new net.Socket(); 

client.connect(8080, "192.168.0.7"); 

console.log("Client most likely connected..."); 


oracledb.getConnection(
    { 
     user   : dbConfig.user, 
     password  : dbConfig.password, 
     connectString : dbConfig.connectString 
    }, 
    function(err, connection) { 
     if (err) { 
     console.error(err.message); 
     return; 
     } 
     client.on('data', function (data) { 
     var weight_data = Number(data); 
     console.log('Data: ' + data); 
     connection.execute("INSERT INTO UNI_SCRAP_SCALE(WEIGHT) VALUES (:weight)", [weight_data], function (err, result) { 
      if (err) throw err; 
      console.log("Rows inserted: " + result.rowsAffected); 
      console.log('Data received from Db:\n'); 
      console.log(result); 
      connection.commit(
      function (err) { 
       console.log('Done') 
      }); 
      }); 
     }); 
     }); 
    }); 



// client.destroy(); 
+0

だから私は、3つの別々の場所でのsetTimeoutを置きます。 connection.executeがsetTimeoutブロック内にあった場合、コールバックが必要であるとエラーします。だから私は3つのconsole.logsの下に置くことを試み、3秒間そのコードを待ってから、setTimeoutがもう存在しないかのように実行を開始しました(setTimeoutは1回だけです)。明らかに、ノードは貧しい選択でした。たぶんソケットを使用することも悪い選択でした。 –

答えて

0

はJavaScript:

setTimeout(function() { 
    // code you want to wait for here 
}, 3000); 
1

のJavaScript、のsetTimeout()でタイムアウトを設定する機能があり、ここでの例です:

あなたのコードは、3秒後に実行されます
setTimeout(function { 
    // place your code here 
}, 3000); //number of millisecond before executing code 

ドキュメント: https://www.w3schools.com/jsref/met_win_settimeout.asp

+0

関数内でタイムアウトが消えることについての記述は間違っています。 – jfriend00

+0

ああ、もう一度テストして、あなたが正しいです。私はsetInterval()とsetTimeout()がいくつかの関数や他の何かに入れ子になっているケースに直面したと確信しています。私は調査します。 – Nolyurn

関連する問題