Skip to main content
Commonmark migration
Source Link

###Usage Example: Stack Exchange Site Monitor

Usage Example: Stack Exchange Site Monitor

JavaScript Site Monitor in 20 lines of code or less....

Demonstrates the use of Soapi.js to query stackauth.com to get an up-to-date list of Stack Exchange sites and then polling /stats for each one on a revolving timer.

enter image description here

$(document).ready(function siteMonitor() {

    var apiKey = '';
    new Soapi.RouteFactory("", apiKey).Sites().getResponse(function(data) {
        var sites = [];
        for (var i = 0; i < data.items.length; i++) {
            sites.push(data.items[i].api_endpoint.substring(7));
        }

        var siteIndex = 0;
        var delay = 10; // get it started
        var pollingInterval = 20000; // 20 seconds per site, > 1 minute between identical requests

        $.each(sites, function(idx, itm) {
            $('<div/>').addClass('stats').attr('id', itm.replace(/\./g, '')).appendTo(document.body);
        });

        function update() {
            var client = new Soapi.RouteFactory(sites[siteIndex], apiKey)
            .Stats()
            .getResponse(function(data) {
                var id = sites[siteIndex].replace(/\./g, '');
                $('#' + id).hide().fadeIn(100).jqotesub("#template", data.items[0]);
                siteIndex++;
                if (siteIndex == sites.length) {
                    siteIndex = 0;
                    delay = pollingInterval;
                }
                window.setTimeout(update, delay);
            });
        }
        update();
    });
});

###Usage Example: Stack Exchange Site Monitor

JavaScript Site Monitor in 20 lines of code or less....

Demonstrates the use of Soapi.js to query stackauth.com to get an up-to-date list of Stack Exchange sites and then polling /stats for each one on a revolving timer.

enter image description here

$(document).ready(function siteMonitor() {

    var apiKey = '';
    new Soapi.RouteFactory("", apiKey).Sites().getResponse(function(data) {
        var sites = [];
        for (var i = 0; i < data.items.length; i++) {
            sites.push(data.items[i].api_endpoint.substring(7));
        }

        var siteIndex = 0;
        var delay = 10; // get it started
        var pollingInterval = 20000; // 20 seconds per site, > 1 minute between identical requests

        $.each(sites, function(idx, itm) {
            $('<div/>').addClass('stats').attr('id', itm.replace(/\./g, '')).appendTo(document.body);
        });

        function update() {
            var client = new Soapi.RouteFactory(sites[siteIndex], apiKey)
            .Stats()
            .getResponse(function(data) {
                var id = sites[siteIndex].replace(/\./g, '');
                $('#' + id).hide().fadeIn(100).jqotesub("#template", data.items[0]);
                siteIndex++;
                if (siteIndex == sites.length) {
                    siteIndex = 0;
                    delay = pollingInterval;
                }
                window.setTimeout(update, delay);
            });
        }
        update();
    });
});

Usage Example: Stack Exchange Site Monitor

JavaScript Site Monitor in 20 lines of code or less....

Demonstrates the use of Soapi.js to query stackauth.com to get an up-to-date list of Stack Exchange sites and then polling /stats for each one on a revolving timer.

enter image description here

$(document).ready(function siteMonitor() {

    var apiKey = '';
    new Soapi.RouteFactory("", apiKey).Sites().getResponse(function(data) {
        var sites = [];
        for (var i = 0; i < data.items.length; i++) {
            sites.push(data.items[i].api_endpoint.substring(7));
        }

        var siteIndex = 0;
        var delay = 10; // get it started
        var pollingInterval = 20000; // 20 seconds per site, > 1 minute between identical requests

        $.each(sites, function(idx, itm) {
            $('<div/>').addClass('stats').attr('id', itm.replace(/\./g, '')).appendTo(document.body);
        });

        function update() {
            var client = new Soapi.RouteFactory(sites[siteIndex], apiKey)
            .Stats()
            .getResponse(function(data) {
                var id = sites[siteIndex].replace(/\./g, '');
                $('#' + id).hide().fadeIn(100).jqotesub("#template", data.items[0]);
                siteIndex++;
                if (siteIndex == sites.length) {
                    siteIndex = 0;
                    delay = pollingInterval;
                }
                window.setTimeout(update, delay);
            });
        }
        update();
    });
});
Replace bad ImageShack images per http://meta.stackexchange.com/q/263771/
Source Link
Brock Adams
  • 13k
  • 5
  • 39
  • 64

###Usage Example: Stack Exchange Site Monitor

JavaScript Site Monitor in 20 lines of code or less....

Demonstrates the use of Soapi.js to query stackauth.com to get an up-to-date list of Stack Exchange sites and then polling /stats for each one on a revolving timer.

alt text http://img267.imageshack.us/img267/1381/sitemonitor.pngenter image description here

$(document).ready(function siteMonitor() {

    var apiKey = '';
    new Soapi.RouteFactory("", apiKey).Sites().getResponse(function(data) {
        var sites = [];
        for (var i = 0; i < data.items.length; i++) {
            sites.push(data.items[i].api_endpoint.substring(7));
        }

        var siteIndex = 0;
        var delay = 10; // get it started
        var pollingInterval = 20000; // 20 seconds per site, > 1 minute between identical requests

        $.each(sites, function(idx, itm) {
            $('<div/>').addClass('stats').attr('id', itm.replace(/\./g, '')).appendTo(document.body);
        });

        function update() {
            var client = new Soapi.RouteFactory(sites[siteIndex], apiKey)
            .Stats()
            .getResponse(function(data) {
                var id = sites[siteIndex].replace(/\./g, '');
                $('#' + id).hide().fadeIn(100).jqotesub("#template", data.items[0]);
                siteIndex++;
                if (siteIndex == sites.length) {
                    siteIndex = 0;
                    delay = pollingInterval;
                }
                window.setTimeout(update, delay);
            });
        }
        update();
    });
});

###Usage Example: Stack Exchange Site Monitor

JavaScript Site Monitor in 20 lines of code or less....

Demonstrates the use of Soapi.js to query stackauth.com to get an up-to-date list of Stack Exchange sites and then polling /stats for each one on a revolving timer.

alt text http://img267.imageshack.us/img267/1381/sitemonitor.png

$(document).ready(function siteMonitor() {

    var apiKey = '';
    new Soapi.RouteFactory("", apiKey).Sites().getResponse(function(data) {
        var sites = [];
        for (var i = 0; i < data.items.length; i++) {
            sites.push(data.items[i].api_endpoint.substring(7));
        }

        var siteIndex = 0;
        var delay = 10; // get it started
        var pollingInterval = 20000; // 20 seconds per site, > 1 minute between identical requests

        $.each(sites, function(idx, itm) {
            $('<div/>').addClass('stats').attr('id', itm.replace(/\./g, '')).appendTo(document.body);
        });

        function update() {
            var client = new Soapi.RouteFactory(sites[siteIndex], apiKey)
            .Stats()
            .getResponse(function(data) {
                var id = sites[siteIndex].replace(/\./g, '');
                $('#' + id).hide().fadeIn(100).jqotesub("#template", data.items[0]);
                siteIndex++;
                if (siteIndex == sites.length) {
                    siteIndex = 0;
                    delay = pollingInterval;
                }
                window.setTimeout(update, delay);
            });
        }
        update();
    });
});

###Usage Example: Stack Exchange Site Monitor

JavaScript Site Monitor in 20 lines of code or less....

Demonstrates the use of Soapi.js to query stackauth.com to get an up-to-date list of Stack Exchange sites and then polling /stats for each one on a revolving timer.

enter image description here

$(document).ready(function siteMonitor() {

    var apiKey = '';
    new Soapi.RouteFactory("", apiKey).Sites().getResponse(function(data) {
        var sites = [];
        for (var i = 0; i < data.items.length; i++) {
            sites.push(data.items[i].api_endpoint.substring(7));
        }

        var siteIndex = 0;
        var delay = 10; // get it started
        var pollingInterval = 20000; // 20 seconds per site, > 1 minute between identical requests

        $.each(sites, function(idx, itm) {
            $('<div/>').addClass('stats').attr('id', itm.replace(/\./g, '')).appendTo(document.body);
        });

        function update() {
            var client = new Soapi.RouteFactory(sites[siteIndex], apiKey)
            .Stats()
            .getResponse(function(data) {
                var id = sites[siteIndex].replace(/\./g, '');
                $('#' + id).hide().fadeIn(100).jqotesub("#template", data.items[0]);
                siteIndex++;
                if (siteIndex == sites.length) {
                    siteIndex = 0;
                    delay = pollingInterval;
                }
                window.setTimeout(update, delay);
            });
        }
        update();
    });
});
updated code samples to rc2.2
Source Link
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60
$(document).ready(function siteMonitor() {

    var apiKey = 'qgAq_KfDu0KYzlNG-qaTuw';

    // get a list of stack exchange sites from stackauth.com'';
    new Soapi.RouteFactory("", apiKey).ApiSitesSites().getResponse(function(data) {
        var sites = [];
        for (var i = 0; i < data.api_sitesitems.length; i++) {
            sites.push(data.api_sites[i]items[i].api_endpoint.substring(7));
        }

        var siteIndex = 0;
        var delay = 10; // get it started
        var pollingInterval = 20000; // 20 seconds per site, > 1 minute between identical requests

        $.each(sites, function(idx, itm) {
            $('<div/>').addClass('stats').attr('id', itm.replace(/\./g, '')).appendTo(document.body);
        });

        function update() {
            var client = new Soapi.RouteFactory(sites[siteIndex], apiKey)
            .Stats()
            .getResponse(function(data) {
                var id = sites[siteIndex].replace(/\./g, '');
                $('#' + id).hide().fadeIn(100).jqotesub("#template", data.statistics[0]items[0]);
                siteIndex++;
                if (siteIndex == sites.length) {
                    siteIndex = 0;
                    delay = pollingInterval;
                }
                window.setTimeout(update, delay);
            });
        }
        update();
    });
});
$(document).ready(function siteMonitor() {

    var apiKey = 'qgAq_KfDu0KYzlNG-qaTuw';

    // get a list of stack exchange sites from stackauth.com
    new Soapi.RouteFactory("", apiKey).ApiSites().getResponse(function(data) {
        var sites = [];
        for (var i = 0; i < data.api_sites.length; i++) {
            sites.push(data.api_sites[i].api_endpoint.substring(7));
        }

        var siteIndex = 0;
        var delay = 10; // get it started
        var pollingInterval = 20000; // 20 seconds per site, > 1 minute between identical requests

        $.each(sites, function(idx, itm) {
            $('<div/>').addClass('stats').attr('id', itm.replace(/\./g, '')).appendTo(document.body);
        });

        function update() {
            var client = new Soapi.RouteFactory(sites[siteIndex], apiKey)
            .Stats()
            .getResponse(function(data) {
                var id = sites[siteIndex].replace(/\./g, '');
                $('#' + id).hide().fadeIn(100).jqotesub("#template", data.statistics[0]);
                siteIndex++;
                if (siteIndex == sites.length) {
                    siteIndex = 0;
                    delay = pollingInterval;
                }
                window.setTimeout(update, delay);
            });
        }
        update();
    });
});
$(document).ready(function siteMonitor() {

    var apiKey = '';
    new Soapi.RouteFactory("", apiKey).Sites().getResponse(function(data) {
        var sites = [];
        for (var i = 0; i < data.items.length; i++) {
            sites.push(data.items[i].api_endpoint.substring(7));
        }

        var siteIndex = 0;
        var delay = 10; // get it started
        var pollingInterval = 20000; // 20 seconds per site, > 1 minute between identical requests

        $.each(sites, function(idx, itm) {
            $('<div/>').addClass('stats').attr('id', itm.replace(/\./g, '')).appendTo(document.body);
        });

        function update() {
            var client = new Soapi.RouteFactory(sites[siteIndex], apiKey)
            .Stats()
            .getResponse(function(data) {
                var id = sites[siteIndex].replace(/\./g, '');
                $('#' + id).hide().fadeIn(100).jqotesub("#template", data.items[0]);
                siteIndex++;
                if (siteIndex == sites.length) {
                    siteIndex = 0;
                    delay = pollingInterval;
                }
                window.setTimeout(update, delay);
            });
        }
        update();
    });
});
added 542 characters in body
Source Link
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60
Loading
added 6 characters in body
Source Link
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60
Loading
added 14 characters in body
Source Link
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60
Loading
Source Link
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60
Loading