0

I would like to run a script that repeats 100 times.

With each repetition it should be in order

Take the names from an array or list and pass them to my variable. How can I do this ?

I created this as an example and think about how I can install a function there. I've read and tried a few things. But somehow I'm not ready yet

var TestNames = ["test1", "test2", "test3", "test4", "test5"]
var n = TestNames[0]
interval = setInterval(function() {
  var Username = n;
  console.log(Username);
  // here is some code                           
}, 2000);
setTimeout(function() {
  clearInterval(interval)
}, 10000);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Jeffrey
  • 3
  • 3
  • What is the logic to pick a name in each iteration? Is it random or by order? Does [`var Username = TestNames.shift();`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift) suffice? What happens when all names are iterated over? – Lain Jan 17 '23 at 09:25

3 Answers3

2

Aassuming the setTimeout is calculcated by the amount of names (10000/2000), you can omit it entirely.

const TestNames = ["test1", "test2", "test3", "test4", "test5"];

//REM: https://developer.mozilla.org/en-US/docs/Web/API/setInterval
//REM: Passing a copy of TestNames to not alter the original
const interval = setInterval(function(listOfNames){ 
  if(listOfNames.length){
    //REM: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
    const Username = listOfNames.shift();
    console.log('Current name:', Username);
    console.log(TestNames.length - listOfNames.length, ' names done.');
    console.log(listOfNames.length % TestNames.length, ' names left.')
  }
  else{
    //REM: Cancel the interval, if listOfNames has no more items
    clearInterval(interval);
    console.log('No names left.')
  }
}, 2000, Array.from(TestNames));

Take the names from an array or list and pass them to my variable. How can I do this ?

That depends on your requirements.

  • Do you need to keep TestNames?
  • Do you pick a random name?
  • Do you just pick the next name in order?

In my example I pass a copy of TestNames to the interval as listOfNames. On each iteration I remove the first item from listOfNames by using shift which also returns that removed item and assign it to Username. If listOfNames has no more items, the interval gets cleared.

Lain
  • 3,657
  • 1
  • 20
  • 27
0

const TestNames = ["test1", "test2", "test3", "test4", "test5"]

let index = 0

const interval = setInterval(function() {

  const Username = TestNames[index];

  console.log(Username);
  index += 1
}, 2000);


setTimeout(function() {
  clearInterval(interval)
}, 10000);
Konrad
  • 21,590
  • 4
  • 28
  • 64
0

If you want to write a username for each iteration, I suggest this. Also note the use of const and let

const testNames = ["test1", "test2", "test3", "test4", "test5"]; // JS arrays starts at 0
const numberOfNames = testNames.length;
let counter = 0;
let interval = setInterval(function() { 
  if (counter >= numberOfNames) return; // don't continue
  let Username = testNames[counter];
  console.log(counter,Username); 
  counter++; // point at the next or perhaps beyond - shorthand for counter = counter + 1 or counter += 1.  ++ increments

  // here is some code                           
}, 2000);
let tId = setTimeout(function() {
  clearInterval(interval)
}, 10000);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Is there any reason for `interval` to be `let` and to assign `setTimeout` to `tId`? Just curious about the choice. – JavaScript Jan 17 '23 at 10:00
  • Using `++` is an antipattern for me. `testNames[counter++]` would make more sense. When you use `++` you imply that you care about the current value, but you want to increment it after. – Konrad Jan 17 '23 at 10:00
  • @Konrad while that is a stance to take, I entirely disagree. I totally dislike those **assign and use variable in same statement**. It is like using `testNames[index += 1]` in your snippet (if you start at -1). Also it may work in this case, yet it might not work generally if `counter` has more uses. – JavaScript Jan 17 '23 at 10:07
  • @JavaScript I always assign setInterval and setTimeout to separate variables. And I always assign them to a variable to be sure I can clear them if needed. – mplungjan Jan 17 '23 at 10:10
  • I was contemplating the [counter++] but I prefer to set it explicitly. In this case even more important because I wanted show the counter in the console.log BEFORE incrementing to teach SO that the JS Arrays started a 0. Also I ALWAYS use ++ when adding one to anything – mplungjan Jan 17 '23 at 10:12
  • 2
    Thanks again to everyone who helped :-) For my purposes, the code of "Gelegen" works best. Since I can always enlarge my array. So I don't need to know how many names are in it to calculate setTimeout. I like the counter function of "mplungJan". In the end I would know how many names I have without counting them myself. Now I'll try to see if I, as a beginner, can integrate the counter function in "Gelegen" his code :-) – Jeffrey Jan 17 '23 at 10:45