如何在jQuery每个函数中仅返回一次异常消息?

时间:2019-05-08 04:30:00

标签: javascript jquery bootstrap-4

我使用"Tempus Dominus Bootstrap 4"进行时间操作。

今天,我已经实现了一个功能,当按下某个按钮时可以清除所有输入值。

但是,此插件中有一个错误,执行清除功能时似乎发生了错误。

但是,我现在没有时间解决它,所以我想抛出一个异常并继续处理。

另一方面,控制台窗口出现错误,我感到不舒服。

所以我只想第一次抛出一次异常。

因此,我编写了以下代码。

var errorStack = 0;

$('.btn_clear_daterange').click(function(){
  var target = $(this).data('target'); // e.g. ".daterange_fields"
  $(target).each(function(){
    try {
      $(this).datetimepicker('clear');
    }
    catch(e){
      errorStack++;

      if(errorStack === 1){
        console.warn("clear function has a issue. \n check the follow link: https://github.com/tempusdominus/bootstrap-4/issues/34");
        console.error(e);
      }
    }
  });
});

我希望这样做。

$('.btn_clear_daterange').click(function(){
  var target = $(this).data('target');
  $(target).each(function(){
    try {
      $(this).datetimepicker('clear');
    }
    catch(e){
      return e;
    }
  });

  if(e && e.count === 1){
    console.warn("clear function has a issue. \n check the follow link: https://github.com/tempusdominus/bootstrap-4/issues/34");
    console.error(e);
  }
});

我该怎么办?谢谢。

2 个答案:

答案 0 :(得分:0)

$('.btn_clear_daterange').click(function () {
    var target = $(this).data('target');
    var error = null;
    $(target).each(function () {
        try {
            $(this).datetimepicker('clear');
        }
        catch (e) {
            error = e;
            return false; // break each https://stackoverflow.com/questions/1784780 
        }
    });

    if (error  && error.count === 1) {
        console.warn("clear function has a issue. \n check the follow link: https://github.com/tempusdominus/bootstrap-4/issues/34");
        console.error(e);
    }
});

答案 1 :(得分:0)

在这种情况下,您可以玩UpdateAllBookingJourneyDetails标志。尝试这样。

boolean