Timeout Function Used As A Wait/Delay - Javascript - N8000 Series

While programming your system with an N8000 series device you may come across an instance where a device needs the code to delay, or timeout as we term it. Here's the code and best practices that you can go by to put into your program.

Here's the code format:

setTimeout(function() {<myModule>.<myCommand>;}, <Delay Interval>);

In this instance you would replace <myModule> with whatever your constant is set to for the device you are sending your command to and you would also replace <myCommand> with the command that you wish to delay. <Delay Interval> will be replaced with the length of time, in milliseconds, that you would like the command to wait/delay before executing.

***Keep in mind that the only practical use for this methodology would be in the case of RS232 or IR commands that an end device (eg Display or Projector) does not respond properly to a fast sequence of commands. This should NOT be used for standard coding protocol and best practices in your code. In most cases, this process is unnecessary.***

You can execute several timeouts within the same button press or instance, however they do not execute and wait in sequence as some programmers expect from working with other interfaces. In our solution, you will need to use the timeout code and the subsequent time considerations as independent wait commands and plan accordingly. They do not automatically wait for each previous command to timeout nor do the delay times stack from line to line.

For example, if you wish to send a command immediately, then the second command to wait for 1 second before executing, then the 3rd command to wait for 1 second after the 2nd command, and finally a 4th command to wait for 5 seconds after the 3rd command, then you would have to write your code like this:

<myModule>.<myCommand>;

setTimeout(function() {<myModule>.<myCommand>;}, 1000);

setTimeout(function() {<myModule>.<myCommand>;}, 2000);

setTimeout(function() {<myModule>.<myCommand>;}, 7000);

Notice how the 3rd and 4th commands added the previous command's time interval together. If you were to mistakenly put commands 2 and 3 at 1000, they would attempt to execute at the same time and could potentially not be sent properly to your end device.

In this example, you'll see that in the setup script, my constant is called myDec which defines my decoder and its IP address. Then it is referenced in the timeout code on the subsequent button. This example shows a button press that will toggle my decoder to mute and unmute (audioOff and audioOn commands) my decoder at set time intervals.

Project Setup Script:

Setup_Script.JPG

Example Button with Timeout Code:

TimeOut_Code.JPG

When I execute this button press, it delays for 1 second, then switches between a mute and unmute state every subsequent 1 second afterwards for 6 seconds.

This timeout function can be a very useful tool when running into programming scenarios that require a delay, however there could be many other uses for this type of function that we haven't thought of. Feel free to experiment and, of course, if you find out a useful application of this code that we haven't thought of, don't hesitate to comment and share.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk