However, yelling "STOP THAT!" could escalate a situation. I could trigger immediate violence, or give the harasser something to fume about over time, causing future violence.
An ideal solution would be to guide them into performing another activity that is cognitively and physically distinct from what they are doing. This gives the harassed person time to leave the area, and changes the behaviour of the harasser (at least temporarily).
If I was to tell the harasser, "Clean my apartment and rearrange the furniture in an aesthetically-pleasing manner", that would provide a good delay and change in behaviour. However, the harasser is not going to do that.
Presumably, harassers are able to function in society most of the time (or else they would be locked up). Therefore, they comply with many of the usual social conventions. A common social convention is that when a stranger comes and asks for the time or for directions, you stop everything, turn to face the stranger, and provide assistance. The stranger can ask several followup questions ("Oh, that is a nice watch/smartphone - where did you get it?", "Is that the fastest route there with traffic right now?"), prolonging the encounter to several minutes. Asking the time is less likely to get a blunt answer of "I don't know, go away", while asking for directions consumes more time and is more cognitively challenging.
The next time I see harassment or threats, I will ask the harasser what time it is, and then how I could get to the intersection of two far-off streets.