//instantiate global variable for a setinterval call later
var _intervalVar = null;

function ribbon_SaveAndClose() {
    Xrm.Page.data.save();
    _intervalVar = setInterval(function () { if (Xrm.Page.getAttribute("vhacrm_name").getValue() && !Xrm.Page.data.entity.getIsDirty()) { checkforsave(); } }, 50);

}

//this gets called once we know the save completed
function checkforsave() {
    //stop the setinterval loop
    clearInterval(_intervalVar);
    window.top.close();
}