最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loops - JavaScript setInterval immediately run - Stack Overflow

programmeradmin22浏览0评论

I found a solution to run interval in javascript immidiately, not waiting for a first "timeout"

setInterval(function hello() {
  console.log('world');
  return hello;
}(), 2500);

But problem is this solution isn't working is construction like this

(function () {
    window.Banner = {

        doMagic: function () {
            setInterval(function magic() {
                console.log('magic');
                return magic;
            }, 2500);
        }
    }
})();

Banner.doMagic();

I've tried to return Banner method, to name it and return and no success. Point is what in real construction is much more plex, so i can't just rewrite it to "return setTimeout" solution, as offered widely, so it would be really perfect to find what am i doing wrong with this one.

/

I found a solution to run interval in javascript immidiately, not waiting for a first "timeout"

setInterval(function hello() {
  console.log('world');
  return hello;
}(), 2500);

But problem is this solution isn't working is construction like this

(function () {
    window.Banner = {

        doMagic: function () {
            setInterval(function magic() {
                console.log('magic');
                return magic;
            }, 2500);
        }
    }
})();

Banner.doMagic();

I've tried to return Banner method, to name it and return and no success. Point is what in real construction is much more plex, so i can't just rewrite it to "return setTimeout" solution, as offered widely, so it would be really perfect to find what am i doing wrong with this one.

http://jsfiddle/5jawxLnr/3/

Share Improve this question asked Sep 23, 2015 at 10:54 LittlebobbydroptablesLittlebobbydroptables 3,7419 gold badges52 silver badges81 bronze badges 1
  • If you want to skip the first interval, create a closure. So, it would look like this: setTimeout( function hello(){ return function(){ [ code here] }; }(), 250 ); – Ismael Miguel Commented Sep 23, 2015 at 11:05
Add a ment  | 

5 Answers 5

Reset to default 4

Perhaps the most proper way to do it would be to take the whole callback outside of the setInterval call and put it in a separate variable:

(function () {
    window.Banner = {

        doMagic: function () {
            var magic = function() {
                console.log('magic');
            };
            setInterval(magic, 2500);
            magic();
        }
    }
})();

Banner.doMagic();

The effect is the same as your first code, but this way your code is a little cleaner.

Your no longer self-executing the function in your 2nd code snippet. You need to change this to the following:

doMagic: function () {
   setInterval(function magic() {
      console.log('magic');
      return magic;
   }(), 2500);
}

I agree with others though, this isn't the cleanest way to do this and isn't very obvious to the next developer who es along. I remend storing the function in a variable, executing it immediately and then running it in the setInterval also:

doMagic: function () {
   var magic = function magic() {
      console.log('magic');
      return magic;
   }
   magic();
   setInterval(magic, 2500);
}

If you add the parenthesis to the below code part it does

    doMagic: function () {
        setInterval(function magic() {
            console.log('magic');
            return magic;
        }(), 2500);                   // added them here
    }

In your code there is no way to perform the required task, instead follow the below approach:

// Use function to perform the task.
function doTask () {
	console.log("...");
}
// Perform task for the first time.
doTask();
// On interval do task.
setInterval(doTask, 2500);

Try this example

var hello = function() {
  document.write('hello... ');
};
setInterval(hello, 1000);
hello();

发布评论

评论列表(0)

  1. 暂无评论