For me, it's the matter of how many loses have I accumulated in a row, if it's more than what I put into my mind then I know that I will have to walk out of the game before I lose too much. Also, knowing that you have other responsibilities that need those money can help you walk out of the game early or entirely.
I guess this kind of situation depends on the gambler. Normally, when a gambler is faced with consecutive losses on his bets, the first thing that would come to his mind is to bet again in order to recover those losses. Once they gain that advantage, they tend to bet again until they repeat the same cycle over and over again.
The best time to walk away is when you actually run out of money! I mean, if you were to gamble, try your best to bring only a handful amount of money for that day. When you are satisfied with the amount that you brought, the only time that would convince a gambler to run away is when they have exhausted all of their money.
While I do agree that it sounds crazy, but having a goal (i.e. doubling your money for tonight, etc.) can lower your expectations which would lead you to betting over your budget.