zista
zista

Reputation: 197

setInterval issue with multiple ajax requests

I have been reading about setInterval and came up with my own way to send multiple ajax requests from one page. The problem is that once i press button 2 after pressing button 1, it stops sending multiple requests and if i press button 1 again it does not stop previous setInterval() and starts parallel requests. What i am trying to achieve is that when i press button 1 it must only print "1" 30 times and when i press button 2, it must stop printing "1" but print "2" after sleeping for 5 seconds and if i press button 1 again it must again start printing "1" and stop printing "2". Following is my code:

<script language='javascript'>
var request = 0;
var request1 = 0;
$(document).ready(function () {
    $("#hi button").click(function () {

        var id = this.id;
        if(request!=0){
            clearInterval(request);
            request1.abort();
        }
        request = setInterval(do_it,1000,id);           
    });
});

function do_it(id) {
        request1 = $.ajax({ 
        url: "php.php?id=" + id,
        success: function (result) {
            $("#result").html(result);
            }
        });
}
</script>

<div class="hi" id="hi">
    <button id="1" ></button>
    <button id="2" ></button>
    </div>

<div id="result"></div>

PHP code:

<?php
$word='';
include('sql_connect.php');
if($_GET['id'] == '1') {
    $main = '1';

    for($i = 0; $i < 30; $i++) {
        $word .= $main . "<br>";
    }
    echo $word;
}

if($_GET['id'] == '2') {
    $a = 0; 

    while($a != 1) {
        sleep(5);
        $a=1;
    }               
    echo "2";
}
?>

Any help is appreciated. thanks

Upvotes: 2

Views: 1675

Answers (5)

Amit
Amit

Reputation: 11

Use the setTimeout in the following way

    setTimeout(function(){
        do_it(id);
    }, 1000);

Upvotes: 1

zista
zista

Reputation: 197

Looking at above answers helped me find a good solution. This is what i did:

request1 = $.ajax({ 

url: "php.php?id=" + id,
success: function (result) {
    $("#result").html(result);
    do_it(id); 
} });


function do_it(id) {
        //alert(id);
        request1 = $.ajax({ 
        url: "php.php?id=" + id,
        success: function (result) {
            $("#result").html(result);
            request = setTimeout(do_it,1000,id);
            }
        });
}

Although I could still not pass the parameter properly using setTimeout('do_it(' + id + ')', 1000); , it worked for me.

Upvotes: 0

nav
nav

Reputation: 3115

You have to assign the output of clearInterval() to request so it doesnt hit the if statement again: request = clearInterval(request); - this will cause request to equal undefined, therefore i wouldn't assign your variables at the top, do:

var request;
var request1;

...

if(request) {...}

Also im fairly sure the 3rd param for setInterval() is not the arguements you wish to pass to the function you defined - you would do that like:

setInterval(function() {
    do_it(id);
}, 1000);

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

Upvotes: 0

Francis Lewis
Francis Lewis

Reputation: 8980

When setting a function in setInterval, the function should be encased in quotes or you can use an anonymous function (example below is encased in quotes). Also, you're specifying an incorrect third parameter for setInterval.

The third parameter in setInterval is: Lang: Optional parameter to specify the scripting language e.g.: Jscript, Javascript, vbscript.

setInterval(do_it, 1000, id);

Should be

setInterval('do_it(' + id + ')', 1000);

Upvotes: 1

kjo120
kjo120

Reputation: 1

I am not too familiar with jquery (I just use javascript out of the box), but I have dealt with making ajax calls in timed loops in the past. One thing to consider is that if you are making asynchronous ajax calls, then the setInterval/setTimeout will continue to run regardless if the previous ajax responses have been called (unless if you call setTimeout in the response function, then the ajax request and response calls will run synchronously). Or you can just do syncronous ajax calls -- if that makes sense

Upvotes: 0

Related Questions