4

phidgetsライブラリを使用して、nodejsを介して物理デバイスと対話しています。すべてが物理的に接続されています。やりたいのは、オン/オフのタイミングが正確であることを確認することだけです。

setTimoutに関してconsole.logoutを正しく取得することすらできないため、これが問題になります。

基本的に、私は次のことを行おうとしています。

for ( var i = 0; i < 4; i++ ) {
    setTimeout( function(i){
        console.log('Input: "' + i + '", Executed with timeout of ' + i*1000 + 'ms');
    }(i), i*1000 );
};

しかし、私のコンソールは、タイムアウトなしで、以下を吐き出します。瞬時です。

Input: "0", Executed with timeout of 0ms
Input: "1", Executed with timeout of 1000ms
Input: "2", Executed with timeout of 2000ms
Input: "3", Executed with timeout of 3000ms

これは私が望むものからはほど遠いです。

何が起こっているのかについて何かアイデアはありますか?

4

1 に答える 1

4

setTimeout呼び出しで関数を実行しているのは、(i)

に変更します

setTimeout( function(i){
            console.log('Input: "' + i + '", Executed with timeout of ' + i*1000 + 'ms');
        }, i*1000, i );

このようにして、引数を使用して関数'pointer'をsetTimeoutセットアップに渡します。i

PS:時間値以降のすべての引数はi*1000、コールバック関数に引数として渡されます

于 2012-10-19T09:18:50.043 に答える