how to reset my score (javascript)
I have the following code in my javascript file:
$(document).ready(function(){
$("#shuffle, #game_over_shuffle").bind("click", shuffleBoard);
});
开发者_运维问答
This shuffles some things in a div. Now I also have a scoring system that looks like this:
// Ad score
var score = 0;
You can use a hint, but that will reduce your score (cut from a bigger function):
// Remove 500 points when used
score -= 500;
$('#score').text(score);
This all works, but now I want to reset the score to 0 when the suffle is used. I've tried this:
$("#shuffle, #game_over_shuffle").bind("click", shuffleBoard, resetScore);
Note I've added resetScore and made a new function to reset the score:
function resetScore(){
score = 0;
$('#score').text(score);
};
But this doesn't reset the score to 0. What am I missing?
Kind regards, Maurice
================================
Found a solution myself. Instead of creating a new function, I extended an existing one (using the same bind) setting the score to 0. Thanks to all trying to help me!
You can only pass one event handler to the bind
method. You'll have to call those two functions from within an anonymous function:
$("#shuffle, #game_over_shuffle").bind("click", function() {
shuffleBoard();
resetScore();
});
Here's a live example with this code: http://jsbin.com/iresem/edit
If you're using jQuery 1.5 then you can leverage the new Deferred Objects
Something like this:
var deferred = $.Deferred();
deferred.done(shuffleBoard).done(resetScore);
$("#shuffle, #game_over_shuffle").bind("click", function() {
deferred.resolve();
}
Found a solution myself. Instead of creating a new function, I extended an existing one (using the same bind) setting the score to 0. Thanks to all trying to help me!
精彩评论