Skip to content Skip to sidebar Skip to footer

Convert String To Correct Format For JQuery FadeOut

I need to format a string in order to pass it as a duration for jQuery's fadeOut, the string is a CSS variable that I access in my JavaScript. jQuery's fadeOut accepts a number or

Solution 1:

Here is something on the fly, (its late here too... Maye I didn't cover it all)

let arr = ["300ms", "0.9s", ".4s", "3s"]

arr.forEach(time => {

  time = "0" + time;
  //console.log(time)
  if (time.includes(".") || time.endsWith('s') && !time.endsWith('ms')) {
    time = time.replaceAll('s', '');
    time = time * 1000;
  }
  time = parseFloat(time)
  console.log(time)
});

So you should have I think:

clean: function(str) {
  var num = str;

  num = "0" + num;

  if (num.includes(".") || num.endsWith('s') && !num.endsWith('ms')) {
    num = num.replaceAll('s', '');
    num = num * 1000;
  }

  return parseFloat(num);
}

Post a Comment for "Convert String To Correct Format For JQuery FadeOut"