Phi*_*ger 1 jquery events jquery-ui settimeout
我有一些像这样的jQuery/JavaScript:
$("#dialog-coin-flip").dialog({
height: "auto",
width: 400,
autoOpen: false,
modal: false,
draggable: true,
resizable: true,
closeOnEscape: false,
closeText: "Close",
buttons: {
"Flip": function() {
$(this).children("div").html("Flipping...");
var flipResult = coinFlip();
setTimeout($(this).children("div").html(flipResult), 1000);
},
"Close": function() {
$(this).dialog("close");
},
}
});
function coinFlip() {
var flipResult = Math.floor(Math.random() * (1 - 0 + 1) + 0);
if (flipResult === 0) {
return "You flipped a coin and it came up heads.";
}
else if (flipResult === 1) {
return "You flipped a coin and it came up tails.";
}
}
Run Code Online (Sandbox Code Playgroud)
当我点击"翻转"按钮时,我收到消息:
Uncaught SyntaxError: Unexpected identifier
Run Code Online (Sandbox Code Playgroud)
1000ms过后,在Chrome的JavaScript控制台中.
我究竟做错了什么?
您需要在函数中包含代码.
试着用这个:
var $here = $(this);
setTimeout(function() {
$here.children("div").html(flipResult);
}, 1000);
Run Code Online (Sandbox Code Playgroud)
而不是这个:
setTimeout($(this).children("div").html(flipResult), 1000);
Run Code Online (Sandbox Code Playgroud)