How to pass intervalID to interval function in javascript?

谁说胖子不能爱 提交于 2019-12-21 20:26:37

问题


In javascript when I create an interval, I want to stop the interval from inside the function, but I don't want to reference the ID value from outside like this

var y = setInterval(function(){ clearInterval(y); }, 1000);

What I want is to pass a variable similar to this style

setTimeout(function(data){alert(data);}, 1000, "data");

This works for setInterval too, except I can't really pass the id value that's returned by the setInterval function, because it gets created after calling it.

Right now I'm doing a hack like this:

var r = [];
var y = setInterval(function(r){ if (r.length==1) { clearInterval(r[0]); } }, 1000, r);
r.push(y);

Does anyone know the right way?

Thanks


回答1:


You have to use the return value, but that doesn't mean that the variable needs to be accessible to anything else; just encapsulate:

(function() {
    var y = setInterval(function(){ clearInterval(y); }, 1000);
})();

y in the above is only accessible within the outer anonymous function, which only has the interval function and nothing else in it.

You could even give yourself a reusable setInterval wrapper to do it:

function setIntervalWrapper(callback, time) {
    var args = Array.prototype.slice.call(arguments, 1);
    args[0] = setInterval(function() {
        callback.apply(null, args);
    }, time);
}

That will pass the time handle as the first argument, in front of any others you specify. It also has the benefit of supporting those follow-on arguments reliably cross-browser (some older browsers didn't support passing arguments to the callback).

Gratuitous example:

function setIntervalWrapper(callback, time) {
  var args = Array.prototype.slice.call(arguments, 1);
  args[0] = setInterval(function() {
    callback.apply(null, args);
  }, time);
}

var counter = 0;
setIntervalWrapper(function(handle, arg1, arg2) {
  snippet.log("Interval callback called, handle is " + handle + ", args are '" + arg1 + "' and '" + arg2 + "'");
  if (++counter > 5) {
    clearInterval(handle);

  }
}, 500, "a", "b");
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>



回答2:


You can create a new method, which can provide you the desired functionality, like

window.createInterval = function(cb, timeout, data) {
  if (typeof cb !== "function") {
    throw new Error("Please provide a callback.");
  }

  var interval = window.setInterval(function(data) {
    cb(data, interval);
  }, timeout || 0, data);

  return interval;
};

Now this works like

var myInterval = window.createInterval(function(data, interval){
  console.log(data, interval);
}, 1000, "Hello");

which prints Hello and Interval Id (same as myInterval)

window.createInterval = function(cb, timeout, data) {
  if (typeof cb !== "function") {
    throw new Error("Please provide a callback.");
  }

  var interval = window.setInterval(function(data) {
    cb(data, interval);
  }, timeout || 0, data);

  return interval;
};

var myInterval = window.createInterval(function(data, interval) {
  document.write(data + " " + interval + " " + myInterval);
  window.clearInterval(interval);
}, 1000, "Hello");


来源:https://stackoverflow.com/questions/35920606/how-to-pass-intervalid-to-interval-function-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!