Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
Change of pictures over and over by using setInterval

Dears,

I have written some codes to make the pictures keep changing from time to time. I also want to have a fade in effect each of the picture. The program is running as said. However, somehow in its first loop, the browser always can't render the pictures smoothly, and it comes to smooth start in its second loop. Below is my code, could you tell me why and how to correct it. Thanks.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Pictures</title>
<style>
body {
height:100vh;
}

#picture {
background-repeat: no-repeat;
background-position: center;
background-size: 400px 400px;
width:80%;
margin:auto;
height: 80%;
opacity: 0;
}

#picture.fadein {
opacity: 1;
transition: opacity 2s linear;
}

</style>
</head>
<body>
<div id="picture"></div>
<script>
setInterval(changepcs,3000);
var opacity = 0;
var arrindex = 0;
var arr1 = ["p1.jpg","p2.jpg","p3.jpg","p4.jpg","p5.jpg"];
function changepcs() {
if(arrindex == arr1.length){arrindex = 0};
let abc = document.getElementById("picture");
let address = arr1[arrindex];
abc.style.backgroundImage = `url(${address})`;
abc.classList.remove("fadein");
opacity = 
Number(window.getComputedStyle(abc).getPropertyValue("opacity"));
abc.classList.add("fadein");
arrindex++;
}
</script>
</body>
</html>
about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!