0

I have three javascript functions:

function updateTest() {
    if (totalmin == "0") {
        coolvar = "start";
    } else {
        coolvar = "stop";
    }
}
setInterval(updateTest, 1000)

var audvar = setInterval(function () {
    cooltrigger()
}, 60000);

function cooltrigger() {
    updateaudio(coolvar);
}

I need to design another function that checks the state of coolvar for the same interval, but updates html code the once upon coolvar=start. It then should hold and not update this variable for 60 seconds and then returns to the previous state.

function updateaudio(coolvar) {
     var a_str;
     if (coolvar == "start") {
         a_str = '<audio autoplay source src="audio/coolsound.mp3" type="audio/mpeg"></audio>';
     }
     if (coolvar == "stop") {
         a_str = '<!--Coolsound will only go off at 0:00 timer -->';
     }
     document.getElementById('audio_span').innerHTML = a_str;
 }
 //setTimeout(updateaudio, 6000); 

At the moment using the current code, coolvar will be checked once every 60 seconds then update a_str. I wanted to check coolvar every second but update a_str only once upon first triggering updateaudio.

What am I doing wrong? Is there another way to achieve this?

tewathia
  • 6,890
  • 3
  • 22
  • 27
  • That's a spaghetti code, you wrap `cooltrigger` with an empty function just to call `updateadudio`, use `bind` and get rid of all this mess. – gdoron Feb 06 '14 at 08:11
  • @gdoron Or [on](http://stackoverflow.com/questions/8065305/whats-the-difference-between-on-and-live-or-bind)? Why bind? – loveNoHate Feb 06 '14 at 08:13
  • Please provide more details, e.g. How is the totaltime variable updated, how is coolvar declared and most important: what is it that's not working? – Jan Misker Feb 06 '14 at 08:14
  • @gdoron Ooops, wrong page. ;) – loveNoHate Feb 06 '14 at 08:17
  • @dollarvar I think he was talking about the `Function.prototype.bind` method. [Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) – tewathia Feb 06 '14 at 08:17
  • @tewathia, indeed. He's not using jQuery. – gdoron Feb 06 '14 at 08:18
  • @gdoron Yeah, indeed I meant that with wrong page, confusing, just about to get the difference between `bind` and `call`? **Update**: Just found [that](http://stackoverflow.com/questions/15455009/js-call-apply-vs-bind). ;) – loveNoHate Feb 06 '14 at 08:31
  • @Jan-Misker , coolvar is declared as a global variable outside of the updateTest function i.e `coolvar = this;` The totaltime variable is originally `var minutes = date.getMinutes(); if (minutes < 10) { minutes = ('0' + minutes).slice(-2);}` – user3213563 Feb 06 '14 at 08:52
  • @Jan-Misker , coolvar is declared as a global variable outside of the updateTest function i.e `coolvar = this;` The totaltime variable is originally `var minutes = date.getMinutes(); if (minutes < 10) { minutes = ('0' + minutes).slice(-2);}` What's not working - i want to trigger the html5 audio playback (within `a_str`) once upon `coolvar` being set to "start", at the moment the current behaviour is that the mp3 playback restarts for every setInterval=6000, if i decrease the polling time it restarts every second.... i wanted to check every sec when coolvar=stop,then nocheck for next 60 sec – user3213563 Feb 06 '14 at 08:57
  • @gdoron, thankyou for pointing out bind. Can you show how you would get rid of this spaghetti code mess using bind? I'm reading up on the bind function now, cheers. – user3213563 Feb 06 '14 at 09:06
  • I suspect you have a misunderstanding of variable assignment, please edit your question to include your variable declarations and assignments. Or better: make a test case on http://jsfiddle.net – Jan Misker Feb 06 '14 at 17:13

0 Answers0