亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在jquery click函數中等待確認

如何在jquery click函數中等待確認

智慧大石 2023-07-06 09:53:42
您好,我有一個要求,我需要等待確認才能完成。注意:我無法在點擊(大塊)內觸摸我的前輩的以下代碼或將它們包裹起來.then(function(){..});這是我嘗試過但給出錯誤的:async function showConfirmationAndWait(){    $.alertable.confirm('You sure?').then(function() {      return new Promise(function(res,rej){           res({confirmed:true});      });    }, function() {       return new Promise(function(res,rej){           res({confirmed:false});      });      });}  $('#await').on('click',function(){    var promiseObj =  await showConfirmationAndWait(); //throwing error        //...... big code with its own function call that i don't want to touch });問題: 我想等到確認完成而不用包裝老年人的整個代碼.then(function(){..});這是我嘗試過的:async function showConfirmationAndWait(){    $.alertable.confirm('You sure?').then(function() {      return new Promise(function(res,rej){           res({confirmed:true});      });    }, function() {       return new Promise(function(res,rej){           res({confirmed:false});      });      });}  $(function(){    $('#await').on('click',function(){        var promiseObj =  await showConfirmationAndWait(); //commented due to error         console.log('after await promiseObj',promiseObj);             //...... big code with its own function call that i don't want to touch    });}); <link href="https://cdn.rawgit.com/claviska/jquery-alertable/master/jquery.alertable.css" rel="stylesheet"/><script src="https://code.jquery.com/jquery-2.2.4.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.ui.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js"></script><script src="https://rawgit.com/claviska/jquery-alertable/master/jquery.alertable.min.js"></script><button id="await">Show confirmation wait and proceed next line</button>
查看完整描述

1 回答

?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

您需要重寫showConfirmationAndWait 以實際等待確認并返回結果。


還將您的整個處理程序標記為async.


async function showConfirmationAndWait() {

  try {

    await $.alertable.confirm('You sure?');

    return true

  } catch {

    return false

  }

}


$(function() {

  $('#await').on('click', async function() {

    var promiseObj = await showConfirmationAndWait();

    console.log('after await promiseObj', promiseObj);


    //...... big code with its own function call that i don't want to touch

    console.log('Some scary logic goes here. It should be printed after confirmation.');


  });

});

<link href="https://cdn.rawgit.com/claviska/jquery-alertable/master/jquery.alertable.css" rel="stylesheet" />

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

<script src="https://rawgit.com/claviska/jquery-alertable/master/jquery.alertable.min.js"></script>



<button id="await">Show confirmation wait and proceed next line</button>


查看完整回答
反對 回復 2023-07-06
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號