Javascript timer post to php

Hi
i have a javascript time when the timer reaches zero or time up i want to be able to post to a php file how would i do this
this is my code

<script>
		 var end = new Date('<?php echo ''.date_format($date,"Y-m-d H:i:s").''?>');
    var _second = 1000;
    var _minute = _second * 60;
    var _hour = _minute * 60;
    var _day = _hour * 24;
    var timer;
    var content = "";
    function showRemaining() {
        var now = new Date();
        var distance = end.getTime() - now.getTime();
        if (distance < 0) {
	
            clearInterval(timer);
			
	
            document.getElementById('countdown').innerHTML = 'EXPIRED!';

            return;
        }
	
        var days = Math.floor(distance / _day);
        var hours = Math.floor((distance % _day) / _hour);
        var minutes = Math.floor((distance % _hour) / _minute);
        var seconds = Math.floor((distance % _minute) / _second);

        document.getElementById('countdown').innerHTML = days + 'days ';
        document.getElementById('countdown').innerHTML += hours + 'hrs ';
        document.getElementById('countdown').innerHTML += minutes + 'mins ';
        document.getElementById('countdown').innerHTML += seconds + 'secs';

    }

    timer = setInterval(showRemaining, 1000);

		
	
				</script>

Try to use AJAX.

i know i have to use Ajax but not sure how to implement it into my script. i don’t need to post any data from javascript , just need to fire the php file when the timer reaches zero

I use the jQuery library but the principal should be the same in straight JavaScript. Here is a portion of my game script:

    function check_answer_ajax(myData) {
        $checkAns.off('click', check_answer);
        $.ajax({
            type: 'post',
            url: 'game_play_01.php',
            data: myData,
            success: function (result) {
                if (result.correct) {
                    correct += 1;
                    score = score + points;
                    $('.answer' + result.right_answer).css("background-color", "green");
                    $('.answer' + result.right_answer).css("color", "#fff");
                } else if (result.user_answer === 5) {
                    $('.answer' + result.right_answer).css("background-color", "green");
                    $('.answer' + result.right_answer).css("color", "#fff");
                } else if (result.user_answer <= 4) {
                    score = Math.round(score - (points / 4));
                    $('.answer' + result.right_answer).css("color", "#fff");
                    $('.answer' + result.right_answer).css("background-color", "green");
                    $('.answer' + result.user_answer).css("color", "#fff");
                    $('.answer' + result.user_answer).css("background-color", "red");
                }
                displayScore(score);
                clearInterval(timer);
                currentQuestion += 1;
                id = currentQuestion;
                if (currentQuestion <= totalQuestions) {
                    $nextBtn.slideDown(500);
                    $nextBtn.on('click', reset_display);
                } else {
                    end_of_game();
                }
            },
            error: function (request, status, error) {
            }
        }); // End of ajax function:
    }

You can find more info here: https://github.com/Strider64/Trivia-Quiz-PHP-JQuery-Game

and here’s the actual game: https://www.interactivetrivia.xyz/

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.