How to queue a heartbeat to get faster when an enemy is near

We are making a Slender-type game, and we want to make a function where if the Slender character is getting closer to you, a heartbeat sound will get faster and faster. During the regular part of playing, the heartbeat will be normal, the heartbeat is used to notify the player that Slender is near. how could we pull off this function in Unity?

heartbeat_function(){
heartbeatsound.play();

delay = vector3.distance(player_position, slender_man_position);
delay = delay / 2 to make it happen twice as much

delay = delay * 2 to make it happen half as much

figure out a the right multiplier or divider to get the result you want.

if( Delay > Max_safe_delay)
{
delay = Max_safe_delay
}

yield waitforseconds(delay);