Aslan Kaya
Aslan Kaya

Reputation: 524

Jquery setTimeout constantly calls function multiple times

I have a JQuery code that uses ajax method to call a PHP file and checks if there is any update. If there is no update the timer function kicks in and constantly check the PHP file for an update every 5 seconds. I have manage to get the function working but the time laps is too fast. Even I have changed the interval speed to different parameter it does not show any change. My Jquery Code is:

function CheckCallerID(){
    $.ajax({
        type: 'POST',
        url: "code/PHPFILE.php",
        data: "",                 
        success: function(data) {
            //ajax returns Below HTML CODE
            $('#caller').html(data);

            if($('#newaddressdetails').html().length==0){ 
                var timmer = setTimeout(CheckCallerID(),50000); 
            }else{
                SaveAddress();
            }
        }
    });
}

PHP FILE RETURN VALUE for True <div id="newcustomer">Update Found</div>

PHP FILE RETURN VALUE for False <div id="newcustomer"></div>

Thank you in advance.

Upvotes: 1

Views: 914

Answers (1)

tymeJV
tymeJV

Reputation: 104775

Close - you need to omit the () when passing a function to be called:

var timmer = setTimeout(CheckCallerID,50000); 

When you keep the () in there - it executes the function immediately.

Upvotes: 6

Related Questions