Troubles with making a do while loop work

I’m trying to make a function where a character keep going up as long as you press the jump button and that it hasn’t been pressed for longer than 2 seconds.
But when i call this loop the condition automatically becomes the limit (so here the timer directly becomes 2 when i call the loop) and strangest of all it keeps working even though its while condition is now false (so i can just jump forever)
Here the part of the code where the jump is happening.

if (charactercontroller.isGrounded == true)
        {
            ySpeed = -0.9f;
            jumping = false;
            timer = 0f;

            if(Input.GetKeyDown("space") == true)
            {
                jumping = true;
            }
        }

        if (jumping == true)
        {
            do
            {
                if (Input.GetKey("space") == true)
                {
                    ySpeed = jumpSpeed;
                    timer += Time.deltaTime;
                }
                else
                {
                    jumping = false;
                }
            }
            while (timer <= 2f);

Supposing your code is called in Update:

     if (jumping == true)
     {
             if (Input.GetKey("space") && timer <= 2f)
             {
                 ySpeed = jumpSpeed;
                 timer += Time.deltaTime;
             }
             else
             {
                 jumping = false;
             }
     }