Hello! I'm trying to make a slider for the player, but the problem is that when I create an audio object, I can't call a method inside a function and have to call setInterval twice. Tell me, please, can I somehow fix this?

That is, you have to do this:

 var audio = new Audio(); setInterval("var a = Math.floor(audio.currentTime / (audio.duration / 200))", 500); setInterval(function(){ $('#timeline').css({"width" : "" + a + "px"}); }, 500); 

Instead of doing this:

 var audio = new Audio(); setInterval(function(){ var a = Math.floor(audio.currentTime / (audio.duration / 200)); $('#timeline').css({"width" : "" + a + "px"}); }, 500); 
  • one
    what a meaningful title of the question ... but oh well, why doesn't the second code actually work for you? - Specter
  • Exactly I do not know. ))) I thought that probably you can not use the methods of objects that were created outside of the function - green93
  • and the console prompts something? - Specter
  • window.onload? $ (function () {})? no, not heard - Zowie
  • No, I have not heard and what to do about it? - green93

1 answer 1

And if you do this (so that in one circuit it was):

 (function(){ var audio = new Audio(); setInterval(function(){ var a = Math.floor(audio.currentTime / (audio.duration / 200)); $('#timeline').css({"width" : "" + a + "px"}); }, 500); }()); 
  • Cheto doesn't work either. - green93