Hi, i wanted to display the metacritic score of the games in the homepage near their names.
(Check the image below to see what i see now.)

I've used the unofficial metacritic api at this page API and this is my code :

$(document).ready(function() {
    $('.giveaway__heading__name').each(function() {
        var ga = $(this);

        var t = ga.text();
        if(t.toLowerCase().indexOf('edition') > -1) {
           t = t.substring(0, t.indexOf(':'));
        }

        $.ajax({
            url: "https://byroredux-metacritic.p.mashape.com/find/game",
            type: "POST",
            headers: {
                "X-Mashape-Key": key
            },
            data: {
                title: t,
                platform: "0"
            },
            dataType: "json"

        }).then(function(data) {
            var score = data.result.score;
            if (score) {
                ga.append('<span style="margin-left:7px;color:#A5D277">(' + score + ')<span>');
            }
        });
    });
});

(Sorry if my code is bad, i don't have so much experience with web development)

Due all the ajax calls the code is slow and it takes a few seconds to load the scores and i don't think those api have an option for asking for multiple games at once.

Do you have any suggestion on how to improve the speed of this script?

Thanks.

View attached image.
9 years ago*

Comment has been collapsed.

ajax isn't too fast at the moment

View attached image.
9 years ago
Permalink

Comment has been collapsed.

If you have a web server, you could cache all the scores into your server and make an api that allows multiple games at once. So instead of calling mashape.com one game at a time, you could call yourserver.com and retrieve the cached data in one single ajax call. You can always refresh your cache using a cron or on-demand.

9 years ago
Permalink

Comment has been collapsed.

I've thought about this but i'm not sure i want to setup a server only for this, in the meanwhile i'm caching using the browser localstorage, it's not so good if i want to use a different browser or pc but in the meanwhile it's better than before.

9 years ago
Permalink

Comment has been collapsed.

That would improve subsequent calls, but for every first time call or when you clear your local storage, it'll be ajax-ing one game at a time again. If you want, I can sponsor a small little web space for you to experiment. Add me if interested.

9 years ago
Permalink

Comment has been collapsed.

thanks, but it would be a waste with me, i tend change what i do frequently and drop everything so probably in few days/weeks i will not even use this anymore.

9 years ago
Permalink

Comment has been collapsed.

Enhanced Steam will add the user metacritic score to the page for you (in the normal place though) if you're intereted in that.

9 years ago
Permalink

Comment has been collapsed.

Sign in through Steam to add a comment.