1

im trying to make responsive voice read out each sentences and there must be a 1 minute gap between each readouts.

                function read(){

             responsiveVoice.speak('قلم','Arabic Female');
            }


            var a = [1,2,3];

            $(a).each( function(){ 

            setTimeout(function(){ read(); }, 1000);

            });

currently it only plays once and in the other two loops getting error

Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().

i also can't make it read anything passed to it dynamically

2 Answers2

0

Maybe problem is that you used .each() method not correctly because $(a) try to target selector but not an array.

From jQuery.each() doc:

jQuery.each( array, callback ) where

array: Type: Array - The array to iterate over.

callback: Type: Function( Integer indexInArray, Object value ) - The function that will be executed on every object.

So, try this:

$.each( a, function(){ 
   setTimeout(function(){ 
      read(); 
   }, 1000);
});

Hope this help you.

DenysM
  • 389
  • 1
  • 2
  • 13
0

OK, after googling a bit i found this answer

setTimeout inside $.each()

Basically, you need to increment the timer.

$.each( a, function(i){ 
   setTimeout(function(){ 
      read(); 
   }, i*1000);
});

So first loop is 1 second. Loop 2 will be 2 second.

Here is my sample code:

setTimeout(function(){
                    console.log(text)
                    responsiveVoice.speak(text, 'Swedish Female', {rate: 0.7}, {pitch: 2});
               }, i*6000);

Each speech takes approx 5 seconds. I use 6 second because i want to add some pause each line.

Granit
  • 188
  • 1
  • 11