Skip to main content

Get the Reddit app

Scan this QR code to download the app now
Or check it out in the app stores

r/learnjavascript

members
online

need help changing images after an interval need help changing images after an interval
const imgs = document.querySelectorAll(".imgs");

setInterval(() => {
  let randomNumb = Math.floor(Math.random() * 3);
  const images = ["img1.jpg", "img2.jpg", "img3.jpg"];

  imgs.forEach((img) => {
    img.src = images[randomNumb];
  });
}, 3000);

how can i alter this code so that i can display 3 different set of images for my 3 image containers. i mean i can repeat the code 3 times and make different arrays for all 3 instances but is there an easier or shorter way of doing it?


40g of protein. 27 vitamins and minerals. A full meal in a bottle... ARE YOU NOT IMPRESSED!?
40g of protein. 27 vitamins and minerals. A full meal in a bottle... ARE YOU NOT IMPRESSED!?


Javascript program compiling but no output Javascript program compiling but no output

Hi.

I wrote the following program in windows 11 environment:

var assert = require('assert')
const path = require("path");
const fs = require("fs");
module.exports = async function(callback)
{
   try{
       first();
   }catch(error){
      console.log(error)
   }//end catch
   callback();
 
function first(){
   console.log("Testing");
}

}//module

The program compiles with node.js :

D:\Javascript> node func1.js

However, no output. Somebody, please guide me.

Zulfi.