How do I disable pathfinding while the target is out of range?

There is such a problem: monster chases the player in a certain space, which is denoted by A* grid, but when the player is not in the range of the monster - the monster chases the player on the edge of the grid. How can I make the monster stop during the loss of the player or return to the initial position?


You can do some check like

void Update () {
     var ai = GetComponent<IAstarAI>();
     if (Vector3.Distance(ai.position, playerPosition) > 10) {
          ai.destination = idlePosition;
     } else {
          ai.destination = playerPosition;