0

I use "Tempus Dominus Bootstrap 4" for time manipulation.

Today, I have been implement a function to clear all input values​when a certain button is pressed.

However, there is a bug in this plugin, it seems an error occurs when executing clear function.

But, I do not have a time to solve it now, so I would like to throw an exception and proceed it.

On the other hand, I am not feeling well that errors cover the console window.

So I want to throw exceptions only once at the first time.

So, I wrote the following code.

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);
      }
    }
  });
});

I hope to do like this.

$('.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);
  }
});

How can I do? thanks.

qwe001
  • 513
  • 9
  • 22

2 Answers2

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);
    }
});
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
Wang Liang
  • 4,244
  • 6
  • 22
  • 45
0

you can play with boolean flags in this case. try like this.

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

    if(error){
        console.warn("clear function has a issue. \n check the follow link: https://github.com/tempusdominus/bootstrap-4/issues/34");
        console.error(e);
    }
});
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59