0.0.3 • Published 5 years ago

test-settimeout-simple v0.0.3

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

test-settimeout-simple

setTimeout() caveats or misunderstanding. What is real timeout value?

Usage:

To install locally go in the root directory of your project and type

npm install test-settimeout-simple --save-dev

or

npm -i test-settimeout-simple --save-dev

Test from console:

npm test <delay> <timeout>

where
delay and timeout are digital variables with meanings:\ timeout - number of milliseconds similar to second argument of JavaScript setTimeout() function.\ delay - presumable calculation time in milliseconds necessary to carry out codes in js-block following after the line where setTimeout() function is calling Example:

npm test 100 500

or

npm test 500 200

To get equivalent tests inside your project module:

var simp = require('test-settimeout-simple').simple;
simp.test(100,500);
simp.test(500,200);

See codes and printing output for details comprehension.

Pretext:

I observed that effective timeout (lag of launching) of argument-function in standard JavaScript setTimeout(fun, timeout) depends on the time necessary to carry out calculation of codes following the line where setTimeout() resides, i.e.

By definition

{
  // ...
  t0 = new Date();
  var cleaner = setTimeout(fun,timeout);  

means that function fun will be launched over approximately timeout milliseconds. Suppose that after this statement we have a block consuming time for it's calculation, like this

 var t = 0, tw;  
  while( t < delay ){    
    tw = new Date();
    t = tw - t0;
  }

'delay' milliseconds will be consumed by this.

The effective timeout over which fun will begin to run will be equal to timeout argument's value only if timeout >= delay

Otherwise the delay's value determines effective timeout.

Testing code used:

var cleaner,
    ob = { 
      tf: undefined  //  fun's run beginning time      
    }; 
/** 
 * setTimeout() function-argument
 */
function fun(){
  var tf = new Date();
  ob.tf = tf.getTime();
  
  console.log('in fun: tf = %s, (tf - t0) = %s',
      ob.tf,
      ob.tf - ob.t0);
 
  clearTimeout(cleaner);
}   
/** 
 * test function 
 * @param {number} delay of process's flow in [milliseconds] 
 * @param {number} timeout value of setTimout()'s second argument 
 */
function test( delay, timeout ){
  console.log('test goes ...');
  var tw,      
      t0 = new Date();
  ob.t0 = t0.getTime();
  
  cleaner = setTimeout(fun,timeout);
  
  var t = 0;  
  while( t < delay ){    // process lag
    tw = new Date();
    t = tw - t0;
  }  
  console.log('finish of test with\n' +
              'delay: %s\n' +
              'timeout: %s\n' +  
              't0 = %s\n' +
              'tw = %s\n' +                                   
              't = %s',
              delay, timeout,
              t0.getTime(),tw.getTime(), t
              );  
}

Table bellow illustrates testing results used codes from repository https://github.com/vuGitH/test-settimeout-simple :

in node.js REPL

delaytimeouteffective timeout
100010001011
100010001007
505056
505056
10020002007
20050005001
500100502
10001001007
1000501007
1000501005
1000501006
2000502006
2000502006
2000502006
4000504006
4000504006
400030004007

in Google Chrome console

delaytimeouteffective timeout
100010001003
100010001001
505051
505052
10020002000
20050005001
500100502
10001001004
1000501002
1000501002
1000501002
2000502001
2000502001
2000502001
4000504003
4000504001
400030004006

in MSE console

delaytimeouteffective timeout
100010001003
100010001004
505053
505056
10020002004
20050005011
500100505
10001001004
1000501006
1000501003
1000501003
2000502005
2000502001
2000502006
4000504003
4000504002
400030004002

Comments or editions Welcome! Vladimir Uralov v.url.node@gmail.com