Delay alternative?

I have a code which measures distance with a sensor, and depending on the data measured performs a task. But for it to work, it requires that the task is delayed by 2 seconds. But by using delay() the whole code stops, which is not what I want.

How can I only delay the task, and not the whole code?

use IF then move on.

How can I only delay the task, and not the whole code?

You can't.

What you have to do is to implement a state machine.
See my :-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

And Robin2's:-
http://forum.arduino.cc/index.php?topic=223286.0

...or, just using the "blink without delay" methodology on its own:

distance = analogRead(distanceInputPin);

if ((distance >= distanceSetting) && (currentMillis - previousMillis >= interval)) {
  // perform your task here
}