This isn't something that will fix the overall issue that Lua has not like, wait functionality, and every single file is required to implement it directly itself if it needs something like it, but. This handle of ontick, along with your iteration over the required number of options, removes any freezing.
local testFunc = {starttime = false, totalOptionsToAdd = 300, optionsAdded = 0, context = false}
testFunc.doTestMenu = function(player, context)
testFunc.optionsAdded = 0
testFunc.context = context
testFunc.starttime = os.time()
end
testFunc.doTestTick = function()
if testFunc.starttime then
if testFunc.optionsAdded ~= testFunc.totalOptionsToAdd then
local maxVal = math.min(testFunc.optionsAdded+10, testFunc.totalOptionsToAdd)
for i=testFunc.optionsAdded+1,maxVal do
local firstMenu = testFunc.context:addOption("test" .. i, nil, nil)
local secondMenu = ISContextMenu:getNew(testFunc.context)
testFunc.context:addSubMenu(firstMenu, secondMenu)
testFunc.optionsAdded = testFunc.optionsAdded + 1
end
else
local endtime = os.time()
print(string.format("cost time: %.4f", endtime - testFunc.starttime)) -- cost time: 0.2440
testFunc.starttime = false
testFunc.context = false
end
end
end
Events.OnTick.Add(testFunc.doTestTick)
Events.OnFillWorldObjectContextMenu.Add(testFunc.doTestMenu)