BENARD Patrick
BENARD Patrick

Reputation: 30985

PHP Infinite Ajax loop : any problems?

My question : Can an infinite loop of ajax call can create problem ?

Given there is a code like :

 ajaxcall();
 function ajaxcall(){

    jQuery.ajax({
        type: "POST",
        url: myurl,
        data: mydata,
        cache: false,
        success: function(data){
            if (something )
            {
                 ajaxcall();
            }
        },
        error: function(){},
        complete: function(){}
    });
 }

As you can see, in executing the ajaxcall, if the something is true, it will execute again the ajaxcall().

In case where, the something condition is everytime true, can it cause some problems in navigator or server or something else ? And in case of 'yes there will be big problem', what are the ways to protect against this ?

Thanks for your attention...

UPDATE AFTER ANSWER

I ask this because in inspecting my WordPress Login page, i was wondering was will happened :

If someone with bad intention, open a navigator inspector to find the name of inputs inside form to generate data [in this case 'log' and 'pwd'], and use this kind of code to find a login [, and after a password] with use of navigator's console.

In making a condition on the returned data.

 sendCheckFormAnswer();
 function sendCheckFormAnswer(){

    jQuery.ajax({
        type: "POST",
        url: myLoginPostUrl,
        data: {log:generatedLogin,pwd:generatedPassword},
        cache: false,
        success: function(data){
            if (data.indexOf(badLoginMessage)>=0)
            {
                 codeTogenerateANewLogin();
                 sendCheckFormAnswer();
            } else {
                 alert(generatedLogin);
            }
        },
        error: function(){},
        complete: function(){}
    });
 }

In Addition , here is my login form page :

<form name="loginform" id="loginform" action="http://www.###########.fr/wp-login.php" method="post">
    <p>
        <label for="user_login">Identifiant<br>
        <input type="text" name="log" id="user_login" class="input" value="" size="20"></label>
    </p>
    <p>
        <label for="user_pass">Mot de passe<br>
        <input type="password" name="pwd" id="user_pass" class="input" value="" size="20"></label>
    </p>
    <p class="forgetmenot"><label for="rememberme"><input name="rememberme" type="checkbox" id="rememberme" value="forever"> Se souvenir de moi</label></p>
    <p class="submit">
        <input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="Se connecter">
        <input type="hidden" name="redirect_to" value="http://www.saint-pierre-de-curtille.fr/wp-admin/">
        <input type="hidden" name="testcookie" value="1">
    </p>
</form>

A bad login and bad password return bad login message. A good login and bad password return a bad password message.

Upvotes: 2

Views: 861

Answers (3)

Mir Adnan
Mir Adnan

Reputation: 884

It depends on what youre trying to do. If you are looking to display a notification message or something similar then you should probably use setTimeout function. So It stops when its false or continues.

Also you should consider using GET instead of POST. Get sends one header and is faster comparatively.

Upvotes: 2

Richard Lindsey
Richard Lindsey

Reputation: 81

Sure it could cause a problem, constantly hitting your server with requests, which is compounded by the number of browsers or other clients that have that page loaded... If you're asking if it'll cause a problem for the browser, the browser won't particularly care that this is happening, you just may see increased wait times for those requests to complete as more clients are hitting your server constantly... To protect against this, I would recommend revisiting the rest you have this constant request hitting the server, and see if there's a better way to handle this sort of thing...

Upvotes: 0

Moritz Laass
Moritz Laass

Reputation: 11

You may want to make the second call with a short setTimeout(). Other than that it is a pretty common technique, the only problem it causes, is to drive up your requests.

Upvotes: 0

Related Questions