In some cases, you need to place a button to Standard Salesforce Layout to invoke the Apex method without any confirmation or close buttons. If you work in Visualforce OnClick JavaScript is the best solution.
Salesforce is known as CRM with a lot of Limits. Because Salesforce Apex runs in a multitenant environment, the Apex runtime engine strictly enforces limits so that runaway Apex code or processes don’t monopolize shared resources. If some Apex code exceeds a limit, the associated governor issues a runtime exception that cannot be handled.