refactored button loading javascript