简体   繁体   中英

How to decrease audio volume while looping in javascript and jquery

I want to control audio volume by decreasing it while function loop, but my below code doesn't seems to work.

var count = 0;
var rotate= 5;
var saudio = $("#saudio").get(0);

function tryitnow(){
count++;
saudio.volume = parseInt(saudio.volume)- parseInt(0.1); 
  if (count != rotate) {
    setTimeout(function() {
      tryitnow()
    }, 1000);

  }
}

parseInt(0.1); will return 0 because it will round the float 0.1 so each time you are reducing the volume by 0

You don't need to parseInt(saudio.volume) since it's already a number too.

So this should work

var count = 0;
var rotate = 5;
var saudio = $("#saudio").get(0);

function tryitnow() {
  count++;
  saudio.volume = saudio.volume - 0.1;
  if (count != rotate) {
    setTimeout(function() {
      tryitnow()
    }, 1000);

  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM