[英]Understanding why callback functions in JavaScript are only executed when everything finishes
[英]JavaScript callback functions not executed
我剛剛開始學習回調函數。 不幸的是,我無法使更改后的示例代碼正常工作。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready( function() {
$(".closebtn").click(function(){
function1(someVariable, function() {
function2(someOtherVariable);
});
});
function function1(param, callback) {
alert("Erste Funktion");
callback();
}
function function2(param) {
alert("Zweite Funktion");
}
})
</script>
當我單擊按鈕時,什么也沒有發生。 有人可以幫忙嗎?
您的示例對我有用。 讓我知道你的想法:
// these need to be defined var someVariable = 'example value'; var someOtherVariable = 'example value'; $(document).ready(function() { $(".closebtn").click(function() { function1(someVariable, function() { function2(someOtherVariable); }); }); function function1(param, callback) { console.log("Erste Funktion"); callback(); } function function2(param) { console.log("Zweite Funktion"); } })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="closebtn">Close</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.