我正在写一个游戏,我有一个调用模态的javascript函数来获取骰子,所以我需要停止执行该函数,直到骰子的结果滚动。我查看了回调和承诺,但无法对其进行排序以获得我需要的功能。这是我的代码。
function docombat()
{
getplayerroll("1d20","initiative")
}
function getplayerroll(dice,reason)
{
$.ajax({
url: "myurl",
type:"post",
data:{dice:dice,reason:reason},
success: function(newHTML, textStatus, jqXHR)
{
$(newHTML).appendTo('body').modal({
escapeClose: false,
clickClose: false,
showClose: false
});
}
});
//something like wait for the dice to be rolled and keep going
}
然后是模态
<div id="diceroll""></div>
modal js
function after_roll(res) { diceroll.innerHTML = res; }
所以我需要在原始函数中等待after_roll()
的东西答案 0 :(得分:1)
我将假设您正在使用jQuery Modal的构造风格。在这种情况下,您应该能够使用自定义事件来实现所需的控制流:
function getplayerroll(dice,reason)
{
$.ajax({
url: "myurl",
type:"post",
data:{dice:dice,reason:reason},
success: function(newHTML, textStatus, jqXHR)
{
$(newHTML).appendTo('body').modal({
escapeClose: false,
clickClose: false,
showClose: false
})
// register the event handler
.on('dice-rolled', onDiceRolled);
}
});
//something like wait for the dice to be rolled and keep going
function onDiceRolled(evt)
{
// carry on with the execution here....
}
}
modal js
function after_roll(res)
{
diceroll.innerHTML = res;
// Trigger the event to activate the continuation
$.modal.getCurrent().$elm.trigger('dice-rolled', { /* stick data here */ });
}