Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8236347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:06:21+00:00 2026-06-07T19:06:21+00:00

So i have a show recent pictures div on my website which i would

  • 0

So i have a “show recent” pictures div on my website which i would to to refresh content every 20 seconds, to show a new picture. The problem is that my current ajax call refreshes itself instantely, not after 20 seconds and when it refreshes it doesnt delete the previous data, so it lists all the 1000+ pictures.

here is my ajax call:

$(window).load(function(){
    var timer = 0;
    for (x =0; x<=20; x++)
    {
        timer++;
        if(timer == 20 || x == 20)
        {
            //create XMLHttpRequest object
            xmlHttpRequest = (window.XMLHttpRequest) ?
                new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");

            //If the browser doesn't support Ajax, exit now
            if (xmlHttpRequest == null)
                return;

            //Initiate the XMLHttpRequest object
            xmlHttpRequest.open("GET", "../php/rotalas.php", true);

            //Setup the callback function
            xmlHttpRequest.onreadystatechange = updt_pictures;

            //Send the Ajax request to the server with the GET data
            xmlHttpRequest.send(null);
        }
        function updt_pictures()
        {
            if(xmlHttpRequest.readyState == 4)
            {
                document.getElementById('friss_kepek').innerHTML = xmlHttpRequest.responseText;
            }
        }
    }

and here is the called php which lists the new files

<?php
$timer = 0;
for($x = 0; $x < 20; $x++)
{
    if($timer == 20 OR $x==20)
    {
        $timer = 0;
        $x= 0;
    }
}

$x = 1;
while($x >=$timer)
{
$imgdir = '../img/blog/img/amator/Amator_thumb/'; //Pick your folder .. images

$i=0;

$dimg = opendir($imgdir);//Open directory
while($imgfile = readdir($dimg))
{
    if( in_array(strtolower(substr($imgfile,-3)),$allowed_types) OR
        in_array(strtolower(substr($imgfile,-4)),$allowed_types) )
        /*If the file is an image add it to the array*/
    {$a_img[] = $imgfile;}
    if ($imgfile != "." && $imgfile!="..")
    {
        $imgarray[$i]=$imgfile;
        $i++;
    }

closedir($imgdir);

$totimg = count($a_img);  //The total count of all the images .. img_coun

for($x=$page*1; $x < $totimg && $x < ($page+1)*1; $x++){
    $rand=rand(0,count($imgarray)-1);
    if($rand >= 0)
    {
        echo '<img class="kep_listaz" src="../img/blog/img/amator/Amator_thumb/'.$imgarray[$rand].'" width="160" height="140">';
    }}

i tried to use sleep(20) at the end of the loop, but it didnt refresh at all.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:06:22+00:00Added an answer on June 7, 2026 at 7:06 pm

    Javascript won’t run the “++” operator once per second – it will run it twenty times, very fast. You should use the setInterval function to get the js to only call every 20 seconds:

    http://www.w3schools.com/jsref/met_win_setinterval.asp

    Change your JS (Untested!!!):

    $(window).load(function(){
        setInterval(function(){
            //create XMLHttpRequest object
            xmlHttpRequest = (window.XMLHttpRequest) ?
                new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
    
            //If the browser doesn't support Ajax, exit now
            if (xmlHttpRequest == null)
                return;
    
            //Initiate the XMLHttpRequest object
            xmlHttpRequest.open("GET", "../php/rotalas.php", true);
    
            //Setup the callback function
            xmlHttpRequest.onreadystatechange = function(){
                if(xmlHttpRequest.readyState == 4){
                    document.getElementById('friss_kepek').innerHTML = xmlHttpRequest.responseText;
                }
            };
    
            //Send the Ajax request to the server with the GET data
            xmlHttpRequest.send(null);
        }, 20000); //Run every 20000ms
    }
    

    Edit: and your PHP should be something like this:

    $imgdir = '../img/blog/img/amator/Amator_thumb/'; 
    
    $i=0;
    
    $dimg = opendir($imgdir);//Open directory
    while($imgfile = readdir($dimg))
    {
        if( in_array(strtolower(substr($imgfile,-3)),$allowed_types) OR
            in_array(strtolower(substr($imgfile,-4)),$allowed_types) )
            /*If the file is an image add it to the array*/
        {$a_img[] = $imgfile;}
        if ($imgfile != "." && $imgfile!="..")
        {
            $imgarray[$i]=$imgfile;
            $i++;
        }
    }
    closedir($imgdir);
    
    $totimg = count($a_img);  //The total count of all the images .. img_coun
    
    for($x=$page*1; $x < $totimg && $x < ($page+1)*1; $x++){
        $rand=rand(0,count($imgarray)-1);
        if($rand >= 0)
        {
            echo '<img class="kep_listaz" src="../img/blog/img/amator/Amator_thumb/'.$imgarray[$rand].'" width="160" height="140">';
        }
    }
    

    I haven’t tested this at all, but the general idea should be there. The javascript uses setInterval to make an AJAX call every 20s, then the PHP instantly responds with some images. It’s up to you to figure out how to get just the images you want.

    The thing to take away from this is: Don’t use for loops for timing! That’s something called CPU limiting which is something that shouldn’t be done unless you really really know what you’re doing.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

n00b problem- I am trying to have a list show the most recent entry
I have some text in a UITextView, that I would like to have show
I have a Show table, and I would like to have a derived type
on an asp.net-mvc website page, i want to show recent blog posts from a
I have a Django site in which I show a success message to the
I have photos which have_many comments. I want to select whatever photos have recent
I have an image+text slider with recent blogposts which loop through the recent posts
Hi on our dev environment we have show all errors, warnings and notices. I'm
I have to show slideshow of images. But at one moment I know only
i have to show 3 columns in List view. I tried having table inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.