Skip to main content
Inspiring
November 11, 2016
Question

Why isn't this Javascript working?

  • November 11, 2016
  • 1 reply
  • 700 views

Hello,

I am using Captivate 9.

I am trying to have the project pause when the user opens the TOC, and play again when he closes it or makes a selection in the toc.

Here is the code:

window.addEventListener("moduleReadyEvent", function(evt) {          window.cpAPIEventEmitter.addEventListener('CPAPI_VARIABLEVALUECHANGED',function(e){                                      if(e.Data.oldVal===0){               

          window.cpAPIInterface.setVariableValue("cpCmndResume", 0);           

          window.cpAPIInterface.setVariableValue("cpCmndPause", 1);           

          console.log('opened TOC')                         }        

     else if(e.Data.oldVal===1){               

          window.cpAPIInterface.setVariableValue("cpCmndResume", 1);            

          window.cpAPIInterface.setVariableValue("cpCmndPause", 0);            

          console.log('closed TOC')                   }    

},'cpCmndTOCVisible') });

The right if-conditions are true when they are suppposed to be, but I can't seem to the playbar to stop and continue.

This topic has been closed for replies.

1 reply

TLCMediaDesign
Inspiring
November 11, 2016

It's because the 'CPAPI_VARIABLEVALUECHANGED' event only works with "info" variables, not "cmnd" variables.

You would need to add an event listener to the toc buttons and toggle that way. This code will do the trick. It doesn't listen for a TOC button on the playbar, only the Expand/Collapse Icons at the top:

window.addEventListener( 'moduleReadyEvent', function ( e )
{
var tocIcons = document.getElementsByClassName('tocExpandCollapse')

for ( var i = 0; i < tocIcons.length; i++ )
{
  tocIcons.addEventListener("click", checkTOC, false);
}
});

function checkTOC()
{                                
 
if ( window.cpCmndTOCVisible == true)
{
   window.cpCmndPause = 1;                             
}
else
{
  window.cpCmndResume = 1;                
}
}

Inspiring
November 11, 2016

Great. Thank you!