Hello,
In your code you can use the below statement before starting the calculations and end with another statement.
//Disable for auto update view
XDocument.View.DisableAutoUpdate();
Here is your onload code.
//Enable for auto update view
XDocument.View.EnableAutoUpdate();
Try to reduce unncessary code in onload.