Delay Function In Dev C%2b%2b

Introduction

This function does not work anymore in Dev Cpp; in Borland C, I used to perform the function and I even made my own version of a piano program with just SOUND and DELAY commands. Searching for methods on how I can run the same commands with dos.h or any other include file.

  • Delay in C: delay function is used to suspend execution of a program for a particular time. Declaration: void delay (unsigned int); Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds). To use delay function in your program you should include the 'dos.h' header file which is not a part of standard C library.
  • Dos.h delay function in C: Here, we will learn about the delay function of dos.h header file in C through a simple example/program.
Delay function in dev c 2b 2b tutorial

This article is about delayed execution for a function that we can have in setInterval.

Delay Function In Dev C 2b 2b Programming

We all know, if we want to execute a function or run a task after certain interval of time then we can schedule a call. This can be done in two ways:

Code
  1. setTimeout which allows us to run a function once after the interval of time.
  2. setInterval which allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.

Here, we will look into how we can delay a function execution in setInterval() and see how it behaves.

setInterval

The setInterval method has the same syntax as setTimeout:

All arguments have the same meaning. But unlike setTimeout it runs the function not only once, but regularly after the given interval of time.

Delay Function In Dev C%2b%2b

Let us consider this snippet:

Dev

Observe the output:

It prints the message after 2 seconds continuously.

Delay Function In Dev C 2b 2b Code

It is possible that execution of test function turns out to be longer than we expected and takes more than 2000ms.

what will happen in such case?

I am adding a setTimeout() with a delay of 5000ms which exceeds the interval specified in setInterval().

If you notice the output in your browser console, you would notice the output

after a delay of 7000ms.

And then you could see the following in an interval of 2000ms
received value is : 2
received value is : 3
received value is : 4

In this case the engine waits for test function to complete, then checks the scheduler and if the time is up, runs it again immediately.

Delay Function In Dev C 2b 2b Tutorial

In the edge case, if the function always executes longer than delay ms, then the calls will happen without a pause at all.

Such a case might appear while making an API call inside the function/callback mentioned in setTimeinterval().

Yeah that's about delay in setInterval. Hope this was helpful.

Cheers !!!

Comments are closed.