全て、
私は「クレジットモジュール」(ゲームのクレジットシステムに似ています)を持っています。これは、ユーザーがアクションを実行すると、追加または減算されるコストで内部divを作成し、ユーザーが最後のアクションのコストを確認できるようにします。
問題:関数が1回呼び出される限り、すべてが正常に機能します。ユーザーが複数のアクションをすばやく実行すると、setTimeout関数(コストdivをアニメーション化してから削除することになっています)が実行されません。関数の2番目のインスタンスは、最初のsetTimeout関数をリセットしているようです。
(function()
{
$("#press").on("click", function(){creditCost(50)});
function creditCost(x)
{
var eParent = document.getElementById("creditModule");
// following code creates the div with the cost
eParent.innerHTML += '<div class="cCCost"><p class="cCostNo"></p></div>';
var aCostNo = document.getElementsByClassName("cCostNo");
var eLatestCost = aCostNo[aCostNo.length - 1];
// following line assigns variable to above created div '.cCCost'
var eCCost = eLatestCost.parentNode;
// cost being assigned
eLatestCost.innerHTML = x;
$(eCCost).animate ({"left":"-=50px", "opacity":"1"}, 250, "swing");
// following code needs review... not executing if action is performed multiple times quickly
setTimeout(function()
{
$(eCCost).animate ({"left":"+=50px", "opacity":"0"}, 250, "swing", function ()
{
$(eCCost).remove();
})
}, 1000);
}
})();