How to smooth the echo duration change to get rid of audio clicks? I implemented an echo effect using the following diagrams:


To summarize, an echo effect is basically a positive feedback effect that is implemented with a delay line zD which delays the input signal by D samples. My effects works fine. I get the echo effect.
My issue is I gave the user to adjust D sample amount during runtime just like in Pioneer equiments you can set echo as 4beats, 2beats, 1beats etc. When a user request a beat duration change I immediatly change the D parameter in my audio plugin. The problem is sometimes this causes click due to sudden audio level changes. I am trying to find a way to counter this but so far I can not think of anyways to do it. How may I ensure that the audio level is continous(doesnt spike and result in audio clicks) when user changes the parameter D?
Maybe, when $D$ changes, you can crossfade from the audio at the old $D$ to audio at the new $D$.
If you ran a pitch detector alongside of the delay (and stored the detected period length in a corresponding array) you could crossfade one period at a time until you got to within a period of your new $D$.  Then slide very slowly to the exact $D$.