Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting jQuery's CSS timing to ms

When asking jQuery for a duration in CSS, it returns this value in seconds, e.g. 1.2s. Example here.

var d = $("div"),
    propDur = d.css("animation-duration"),
    propDurArr = propDur.replace(" ","").split(","),
    propDel = d.css("animation-delay"),
    propDelArr = propDel.replace(" ","").split(",");

console.log(propDurArr[propDurArr.length - 1]);
console.log(propDelArr[propDelArr.length - 1]);

Two questions for this:

  1. Is this cross-browser? Does jQuery (or better: the browser) always return the value in seconds, or do some browsers prefer a ms representation?
  2. How do I convert this value in jQuery/JS to ms (without ms)? For example: 1.2s -> 1200.

For 2. you can easily remove the dot, like so:

propDel.replace(/(\s|.)/g,"").split(",")

But how do I make a condition like this:

if  string contains dot
     ADD 00
if  string does NOT contain dot
     ADD 000

Please reply to 1 and 2.

like image 572
Bram Vanroy Avatar asked Mar 26 '26 19:03

Bram Vanroy


1 Answers

I decided to go with @RobG's comment that he hasn't posted as answer yet, so here goes. Quite simple and effective:

function toMS(s) {
    return parseFloat(s) * (/\ds$/.test(s) ? 1000 : 1);
}
like image 113
Bram Vanroy Avatar answered Mar 29 '26 09:03

Bram Vanroy