vmb
vmb

Reputation: 3028

Javascript timer Usage

I have a case in my page,there i have six widgets(ie div) and each one calling one ajax function on different intervals(One div interval may be 20s,othere one it will be 30s).After getting result from server,i will update corresponding panels.Which is the best way to handle this scenario.Is it a good practice to keep six different javascript timers for each of this panels(eg:like setInterval,setTimeout).If not,what is the best way to handle this scenario.

Upvotes: 0

Views: 123

Answers (1)

ATOzTOA
ATOzTOA

Reputation: 35950

Solution:

duration = 20;

(function getMoreData(divIndex) {
    console.log("Process First");

    // Get ajax data for first div
    $.ajax({
        url: url1,
        dataType: 'json',
        data: "",
        timeout: 5000,
        success: function(data) {
                dataFlag[divIndex]= true;

                // Process data...
                // Update "div 1"
            },
        error: function() {
                dataFlag[divIndex]= true;

                // Process error
            }
    });


    // Second div
    console.log("Process Second");


    // Get ajax data for second div
    $.ajax({
        url: url2,
        dataType: 'json',
        data: "",
        timeout: 5000,
        success: function(data) {
                dataFlag[divIndex]= true;

                // Process data...
                // Update "div 2"
            },
        error: function() {
                dataFlag[divIndex]= true;

                // Process error
            }
    });


    // Third div
    console.log("Process Third");


    // Get ajax data for second div
    $.ajax({
        url: url3,
        dataType: 'json',
        data: "",
        timeout: 5000,
        success: function(data) {
                dataFlag[divIndex]= true;

                // Process data...
                // Update "div 3"
            },
        error: function() {
                dataFlag[divIndex]= true;

                // Process error
            }
    });


    setTimeout(getMoreData, (duration * 1000));
})()

Upvotes: 2

Related Questions