John Sims
John Sims

Reputation: 227

jQuery submit form IF

Hey guys, I have this great scripts someone on stack overflow helped me out with, except the one major function I need is missing.

This is a game where the user picks a number (by entering that number in a text field) then hits a play button. After they hit the play button a series of numbers will appear, they then have to click on the number that matches their number.

The query script I'm using counts how many times they hit the number and how many times they missed the number. Take a look at the script in action here. link text

now what I need it to do, is send the score (hits and misses ) to a database after 3 misses, so I can keep high score. Any ideas? Here's the script.

var hitCount = 0,
missCount = 0;

function IsNumeric(n) {
return !isNaN(n);
}

$("#getit").click(function() {
var li = [],
    intervals = 0,
    n = parseInt($('#MyNumber').val());

if (IsNumeric(n)) {
    setInterval(function() {
        li[intervals++ % li.length].text(Math.random() > .1 ? Math.floor(Math.random()        * (10 + n) + (n / 2)) : n).attr('class', '');
    }, <?php echo $time ?>);
}

$('#randomnumber').empty();

for (var i = 0; i < 5; i++) {
    li.push($('<li />').click(function() {
        var $this = $(this);

        if (!$this.hasClass('clicked')) {
            if (parseInt($this.text(), 10) === n) {
                $this.addClass('correct');
                $('#hitcount').text(++hitCount);
            } else {
                $this.addClass('wrong');
                $('#misscount').text(++missCount);
            }
        }

        $this.addClass('clicked');
    }).appendTo('#randomnumber'));
}

return false;
});

Upvotes: 0

Views: 111

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

I've updated your problem here.

After updating the status Check for the missCount if it is greater than or equal to 3 then stop the play(clear the interval and unbind the event), then save the value using an ajax request.

I've changed the event handling to use event delegation instead of direct click event on the <li> elements. Event delegation is better in cases where there are lot of elements to which we have to bind a particular event.

Upvotes: 2

jitter
jitter

Reputation: 54605

I updated your fiddle with the solution. Check http://jsfiddle.net/DHPQT/2/.

I marked all the new stuff with a comment in the form of

//new ....

The main thing to do is checking after

$('#misscount').text(++missCount);

if missCount is 3 and if yes stop the game and send something

Upvotes: 0

Related Questions