Javascript repeat a function x amount of times

javascript repeat function every second
javascript setinterval
javascript settimeout
javascript for loop
js loop x number of times
javascript repeat loop
javascript loop specific number of times
javascript repeat div

I'm trying to develop a function that repeats a function x amount of times, just once, not based on settimerinterval or settimeout or anything based on time. I don't want to use a while/for loop directly, I want to use this repeat function.

I've tried something like this:

function repeat(func, times) {
  for (x = 0; x < times; x++) {
    eval(func)
  }
}

But eval doesn't work on a function.

Just call func and decrement counter and call the function repeat again.

function repeat(func, times) {
    func();
    times && --times && repeat(func, times);
}

repeat(function () { document.write('Hi<br>'); }, 5);

Is there a mechanism to loop x times in ES6 (ECMAScript 6) without , repeat 3 times, the function f, starting with x var result = repeat (3) (f) (x) // is the in short, it evaluates expressions from left to right and returns the value of the might be is there a convenient Range function in (core) JavaScript, something  The repeat() method returns a new string with a specified number of copies of the string it was called on. Browser Support The numbers in the table specify the first browser version that fully supports the method.

const func = () => console.log("hi");
const times = 3;

Array.from({length: times}, () => func());

I define a function. I set the number of times to repeat function. I make an array the size of times to repeat function. I run the "defined function" on each element of the array.

JavaScript: Repeat a function X times at I intervals, I don't see anything problematic with your code, but just for fun, here are some alternative approaches to various parts of the code. It's 100%  I've written a general purpose repeat function which allows you to repeat a callback function X times separated by I intervals with the option to start immediately or after the interval. It can also default to just looping infinitely, which is what setInterval does.

If Lodash is an option, then _.times

What is a simple way to repeat a string in java?, again. const func = () => console. log("hi"); const times = 3; Array. Please note that setInterval executes the function for the first time after delay ms. So if you want to execute function immediately, and THEN repeat every delay, you should do: func(); setInterval(func, delay); – Marco Marsala Aug 6 '14 at 10:48

Using recursion:

function repeat(fn, times) {
  var loop = function (times) {
    if (times) {
      fn(times);
      loop(--times);
    }
  }
  loop(times);
}

repeat(function (times) {
  console.log(times);
}, 5);

JavaScript do/while Statement, statement is used when you want to run a loop at least one time, no matter what. These time intervals are called timing events. The two key methods to use with JavaScript are: setTimeout ( function, milliseconds) Executes a function, after waiting a specified number of milliseconds. setInterval ( function, milliseconds) Same as setTimeout (), but repeats the execution of the function continuously.

String.prototype.repeat(), Parameter, Description. count, Required. The number of times the original string value should be repeated in the new string  This doesn't take X into consideration. If the callback execution time > delay then it will not be fired X times, which some are after. Example: setTimedInterval(callback, 100, 1000); if callback execution is >1 00ms, then it will be probably fired a few times not 10 times. – klodoma Jul 18 '19 at 7:18

JavaScript String repeat() Method, This involves repeating a string a certain number of times. There are the three number. function repeatStringNumTimes(str, num) { return str; }  for (let step = 0; step < 5; step++) { // Runs 5 times, with values of step 0 through 4. console.log('Walking east one step'); } There are many different kinds of loops, but they all essentially do the same thing: they repeat an action some number of times. (Note that it's possible that number could be zero!)

Three ways to repeat a string in JavaScript, What is the correct code for repeating a function or a line of code “x” times? For example: x = 10 (or any variable number obtained) repeat the function function  See the Pen JavaScript Concatenate a specific string for a specific number of times - string-ex-13 by w3resource (@w3resource) on CodePen. Improve this sample solution and post your code through Disqus. Previous: Write a JavaScript function to uncamelize a string.

Comments
  • You don't want to use a loop or a setTimeout. How else can you call something n times? You're basically ruling out all the possibilities to complete your code. Why is a loop no good?
  • func is js function or string ? If function is string how it's defined ( there few ways to define function in js)?
  • Loops aren't allowed apparently... which is nonsense obvs.
  • depending on number, this might be a bad idea, since recursive call are limited : humanwhocodes.com/blog/2009/05/19/…
  • @DiegoDupin, the above has an exit condition.