简体   繁体   English

如何重置我的分数(javascript)

[英]how to reset my score (javascript)

I have the following code in my javascript file: 我的javascript文件中包含以下代码:

$(document).ready(function(){
    $("#shuffle, #game_over_shuffle").bind("click", shuffleBoard);
});

This shuffles some things in a div. 这会改变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. 所有这些都可以,但是现在我想在使用随身行李时将分数重置为0。 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: 注意我添加了resetScore并添加了一个新功能来重置得分:

function resetScore(){
    score = 0;
      $('#score').text(score);
};

But this doesn't reset the score to 0. What am I missing? 但是,这不会将得分重置为0。我想念什么?

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! 我没有创建一个新函数,而是扩展了一个现有函数(使用相同的绑定),将比分设置为0。感谢所有的努力!

You can only pass one event handler to the bind method. 您只能将一个事件处理程序传递给bind方法。 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 这是一个使用此代码的实时示例: http : //jsbin.com/iresem/edit

If you're using jQuery 1.5 then you can leverage the new Deferred Objects 如果您使用的是jQuery 1.5,则可以利用新的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! 我没有创建一个新函数,而是扩展了一个现有函数(使用相同的绑定),将比分设置为0。感谢所有的努力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM