简体   繁体   中英

ResponsiveVoice code within SetInterval in Javascript: How to stop the loop?

Currently I'm doing queuing system and I'm working on voice announcing using responsive voice. I put it on the setInterval but the problem is it is looping and the voice would never stop.

     $( document ).ready(function() {
       setInterval(function() {
         $.ajax({
           url: "/getnewdata",
           cache: false,
         }).done(function(html) {
           $( '#data-container' ).html(html);
         });
         responsiveVoice.speak("{{ $new_call->letter }}-{{ $new_call->number }} please proceed to Counter {{ $new_call->counter_id }}")
       }, 3000);
    });

I just want that voice announcer would only loop once. Another problem, when the data is newly added, it should announce a data. And another problem is instead of for example "no. 30", it speaks "no. 29".

I try this one

$( document ).ready(function() {
    setInterval(function() {
      $.ajax({
        url: "/getnewdata",
        cache: false,
      }).done(function(html) {
          $( '#data-container' ).html(html);
          setTimeout(function() {
            function voiceStartCallback() {
                console.log("Voice started");
            }

            function voiceEndCallback() {
                console.log("Voice ended");
            }

            var parameters = {
                onstart: voiceStartCallback,
                onend: voiceEndCallback
            }
            responsiveVoice.speak("{{ $new_call->letter }}-{{ $new_call->number }} please proceed to Counter {{ $new_call->counter_id }}", parameters)
          }, 800);
      });
    }, 3000);    
  });

When I look at the console it has an error: Uncaught TypeError: Cannot read property 'deprecated' of null at ResponsiveVoice.a.speak (responsivevoice.js?key=8PNDpxy8:117) at queue:178

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