Insomniak
Insomniak

Reputation: 493

post something in django without refreshing (JQuery, AJAX ?)

I am coding a music quiz in django + HTML + JavaScript and I am completely stucked.

What I want to do is to count the time the player made to find the solution. But what I have now is not synchronized because of the loading time of the music. My idea is to send something to django when the player started and to store a time.time() in django at that moment (without refreshing the page !). After that, when the player click on the song he thought it is, it posts a form, I compare a new time.time() with the previous one and it is over.

Everything works very well but I don't know a lot about Jquery and Ajax and I think this is the way to post something without refreshing a page.

My Javascript (this is a timer which is here just to be nice to see but I don't want to post the time calculated on javascript, because, anyone could post the time he wants !) :

function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING){
        //post Start to Django
        setTimeout(function decompte() { 
            setTimeout(function() {
                te--;
                if (te<0) {
                    te = 9; sc--
                }
                time = sc+" s "+te;
                form.time.value=time;
                if (end) {
                    return;
                }
                else if (sc == 0 & te == 0) {
                    zero()
                }
                decompte();
            }, 100);
        },0); //launch the function... Not a very esthetic way to do it but it works !
    }
}

Any ideas ?

Upvotes: 0

Views: 979

Answers (1)

Siva Arunachalam
Siva Arunachalam

Reputation: 7740

It can done with a simple jQuery Post request. There are lots of example given in the following sample django project. It has the proper HTML/jQuery to send the AJAX call and urls.py to receive and myapp/views.py to process and return the response.

https://github.com/sivaa/django-jquery-ajax-exmaples

Upvotes: 1

Related Questions