miércoles, 28 de diciembre de 2016

setTimeout y setInterval con argumentos en la función de llamada y posibilidad de aplicar a objetos

(1) setTimeout:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//------------------------------------------------------------------------------------------------
// Engines      JavaScript 1.5+
//              JScript    5.5+
// Environments NN6+ IE5.5+ MOZILLA1+ SAFARI1+
//------------------------------------------------------------------------------------------------
 
function setTimeoutEx
(
      f // src
    , m // milliseconds
    , o /* target
    , arg1 [, ... [, argN]] */
){
    var i = typeof f;
 
    if (i == 'string' || i == 'function')
    {
        if (!setTimeoutEx.__cnt)
            setTimeoutEx.__cnt = 1;
 
        if (i == 'string')
            f = new Function(f);
 
        if (m == null)
            m = 0;
 
        setTimeoutEx[i = setTimeoutEx.__cnt++] =
        {
              a: Array.prototype.slice.call(arguments, 3)
            , o: o
            , f: f
            , i: setTimeout
                 (
                      'setTimeoutEx[' + i + '].f.apply(setTimeoutEx[' +
                      i + '].o, setTimeoutEx[' + i + '].a)'
                     , m
                 )
        };
 
        return setTimeoutEx[i].i;
    }
 
    return -1;
}
 
//------------------------------------------------------------------------------------------------
 
function clearTimeoutEx (i /* id */)
{
    if (typeof i == 'number' && i > 0)
    {
        clearTimeout(i);
 
        for (var j = 0; j < setTimeoutEx.__cnt; ++j)
            if (setTimeoutEx[j] && setTimeoutEx[j].i == i)
                delete setTimeoutEx[j];
    }
}
(1) Test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
setTimeoutEx
(
      function (s)
      {
          alert(s);
      }
    , 1000
    , null
    , '¡Test!'
);
 
var myobj = {test: 'Ok!'};
 
setTimeoutEx
(
      function (s)
      {
          alert(this.test);
          this.arg1 = s;
      }
    , 1000
    , myobj
    , 'my arg test ok!'
);
(2) setInterval:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//------------------------------------------------------------------------------------------------
// Engines      JavaScript 1.5+
//              JScript    5.5+
// Environments NN6+ IE5.5+ MOZILLA1+ SAFARI1+
//------------------------------------------------------------------------------------------------
 
function setIntervalEx
(
      f // src
    , m // milliseconds
    , o /* target
    , arg1 [, ... [, argN]] */
){
    var i = typeof f;
      
    if (i == 'string' || i == 'function')
    {
        if (!setIntervalEx.__cnt)
            setIntervalEx.__cnt = 1;
 
        if (i == 'string')
            f = new Function(f);
 
        if (m == null)
            m = 0;
 
        setIntervalEx[i = setIntervalEx.__cnt++] =
        {
              a: Array.prototype.slice.call(arguments, 3)
            , o: o
            , f: f
            , i: setInterval
                 (
                      'if (setIntervalEx[' + i + '].f.apply(setIntervalEx['   +
                      i + '].o, setIntervalEx[' + i + '].a))clearIntervalEx(' +
                      'setIntervalEx[' + i + '].i)'
                     , m
                 )
        };
 
        return setIntervalEx[i].i;
    }
 
    return -1;
}
 
//------------------------------------------------------------------------------------------------
 
function clearIntervalEx (i /* id */)
{
    if (typeof i == 'number' && i > 0)
    {
        clearInterval(i);
 
        for (var j = 0; j < clearIntervalEx.__cnt; ++j)
            if (clearIntervalEx[j] && clearIntervalEx[j].i == i)
                delete clearIntervalEx[j];
    }
}
(2) Test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
setIntervalEx
(
      function (s, b)
      {
          alert(s);
          return b;
      }
    , 1000
    , null
    , '¡Test!'
    , true
);
 
var myobj = {test: 'Ok!'};
 
setIntervalEx
(
      function (s)
      {
          alert(this.test);
          this.arg1 = s;
 
          return true;
      }
    , 1000
    , myobj
    , 'my arg test ok!'
);

No hay comentarios:

Publicar un comentario