2017-09-06 47 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

所以我把setTimeout的在3個不同的地點。如果connection.execute位於setTimeout塊內,則會出現錯誤,說明需要回調。所以我試着把它放在3個console.logs的下面,它在那個代碼上等了3秒,然後開始執行,就好像setTimeout不再存在一樣(我想setTimeout只是一次)。顯然,Node是一個糟糕的選擇。也許使用套接字也是一個不錯的選擇。 –

回答

0

JavaScript的:

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

有設置超時在JavaScript中,的setTimeout()的函數,在這裏是一個例子:

setTimeout(function { 
    // place your code here 
}, 3000); //number of millisecond before executing code 

您的代碼將在3秒後執行。

文檔: https://www.w3schools.com/jsref/met_win_settimeout.asp

+0

有關超時在內部函數中消失的說法顯然是錯誤的。 – jfriend00

+0

哦,我再次測試,你是對的。我確信我遇到了一個情況,其中setInterval()和setTimeout()嵌套在某個函數或其他東西中消失。我會調查。 – Nolyurn