setInterval in javascript no sum

Asked

Viewed 43 times

0

Good, I have the following javascript code and when I try to add the strMessage it does not add up, but adds side by side to the value and so on each time it runs, which I am doing wrong?

function main() {

  var s = 0;
  //Espera um segundo
  setInterval(function() {

$.ajax({
  url: 'teste3.php',
  success: function(strMessage) {

    s += strMessage;
  },
});

document.write(s);

  }, 1000);
}

window.onload = main;

  • maybe you would have to make strMessage a number in javascript

  • Convert string to integer.

  • perfect, resulted gave a parseint() and resulted

  • s += parseint(strMessage); ??

  • yes, that’s right

  • there are no decimal numbers?

  • no ,because????

  • if there were parseFloat

  • in which way we won’t leave a question unanswered

  • Hmm ok, actually that might make me miss further ahead

Show 5 more comments

1 answer

2


maybe you would have to make strMessage a number in javascript

s += parseInt(strMessage); 

or to decimal places:

parseFloat()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.