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:
//------------------------------------------------------------------------------------------------
// 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:
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:
//------------------------------------------------------------------------------------------------
// 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:
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