1

I have this code:

wallboard.data.Timer = function () {
    $("div[data-value]").each(function () {
        var time = $(this).attr("data-value");
        if (time > 0) {
            time += 1000;
            $(this).attr("data-value", time).text(TimeToText(time));
        }
    });
}

The function TimeToText() simply takes a millisecond value and output it as hour:seconds (00:00).

The attribute data-value contains a millisecond value and is stores in the variable time.

This is my "debug" output:

var time = $(this).attr("data-value"); time = 4376

if (time > 0) { is true as 4376 is larger than 0

time += 1000; after this "time" is 43761000 - her it starts concatenating the text "4376" and "1000" and this is the proof that the JavaScript engine thinks time is a string type.

How do I make it clear that time should be an integer type?

Espen
  • 3,607
  • 11
  • 48
  • 75
  • 2
    because there is **no integer type** in javascript ? – c69 Nov 05 '14 at 12:51
  • 1
    May be you can use the javascript function Number. E.g. var time = Number($(this).attr("data-value")); – Thangadurai Nov 05 '14 at 12:54
  • possible duplicate of [Javascript String to int conversion](http://stackoverflow.com/questions/18713508/javascript-string-to-int-conversion) – This company is turning evil. Nov 05 '14 at 12:55
  • @Kroltan Yes, it might be, but I also asked the question why - I just forgot it in my body o_O. Why doesn't the vm understand this to be a number? It is pretty obvious to us humans that this is a number, isn't it? – Espen Nov 05 '14 at 12:59
  • @Espen Because that would be a very unpleasant deviation from the convention: in JS you try to modify as little as possible your outputs. If you then wanted to actually concatenate two numeric strings together, you'd have to do lots of hacks to do it. I'll explain more in my answer. – This company is turning evil. Nov 05 '14 at 13:10
  • just parseInt the varriable. – HaveNoDisplayName Nov 05 '14 at 13:33

5 Answers5

3
var time = $(this).attr("data-value");
var timeInt = parseInt(time) + 1000;
TamarG
  • 3,522
  • 12
  • 44
  • 74
1

You need to convert the string retrieved with attr() into a number, e.g.

var time = +($(this).attr("data-value"));
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
1

You can use unary plus operator to convert the string attribute value to a number(you can also use parseInt())

var time = +$(this).attr("data-value");
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

You can use coercion trough the unary +, or just wrap it in a parseInt with a base of 10.

wallboard.data.Timer = function () {
    $("div[data-value]").each(function () {
        var time = parseInt($(this).attr("data-value"), 10);
        if (time > 0) {
            time += 1000;
            $(this).attr("data-value", time).text(TimeToText(time));
        }
    });
}

Also, you could have searched for "javascript string to number" and you would find billions of results.

EDIT: Why not interpret numeric strings as numbers automatically? Because that would be a very unpleasant deviation from the convention: in JS you try to modify as little as possible your outputs. If you then wanted to actually concatenate two numeric strings together, you'd have to do lots of hacks to do it:

Instead of var a = "1000" + "10" to get "100010", you would have to do something like this

var a = ["1000", "zz10"].join(""); // note the "zz", so it's not plain numeric.
a = a.replace("zz", ""); // replace "zz" with nothing.
// now `a` would be "100010"
  • Maybe it make sense. I'm just thinking the vm should differ from time += 1000 and time += "1000". And that this line of code should tip the type scale to make the vm think that time is a number and not a string. But maybe it just can't change a type midway...? – Espen Nov 05 '14 at 13:53
  • @Espen It could, but it's a lot more overhead for something that may generate subtle bugs. Number addition is commutative, string concatenation isn't. It's better to assume that if there are strings in the play, it will be a concatenation. Remember that `a += b` is equivalent to `a = a + b`. Also, it's much more readable to guarantee both sides of the `+` operator are of the same type explicitly. In short, "because the standard says so". – This company is turning evil. Nov 05 '14 at 13:59
1

You should convert the string to integer before adding it with 1000.

var time =  parseInt($(this).attr("data-value"));