Going hungry over a certain period each day can help protect against the risks of a high-fat, high-sugar diet, a study has found.