New-UDDynamic inside New-UDButton

Product: PowerShell Universal
Version: 3.x.x

This is mainly a logistical question : Why does new-UDDynamic act/respond differently when its inside an OnClick of a button as compared to a New-UDCard?
This code works as expected :

    New-UDCard -content { 
        New-UDElement -tag 'div' -Id 'result'

        New-UDDynamic -Id 'event' -content {
            $elapsedTime = [System.Diagnostics.Stopwatch]::StartNew()
            # Real code goes here, timer is a placeholder for something that takes time to process.
            start-sleep -Seconds 5
            # END real code
            Set-UDElement -Id 'result' -Content { 
                New-UDTypography -Text "Job Completed : Time taken $($elapsedtime.Elapsed.ToString('hh\:mm\:ss'))" -Style @{color = 'Green'} 
            }
        }
        New-UDDynamic -Content {
            Set-UDElement -Id 'result' -Content {
                New-UDRow -Columns {
                    New-UDColumn -SmallSize 1 -LargeSize 3 -Content {
                        New-UDProgress -circular -ProgressColor purple -Size large
                    }
                    New-UDColumn -SmallSize 1 -LargeSize 3 -Content {
                        New-UDParagraph -text 'Working..' -Color white
                    }
                }
            }

        }

While similar code inside a button does not.
Without pasting all the code, its layout is such:
Main Menu with a lot of New-UDElements …
Through out the dashboard they are filled as the dashboard processes information. Near the end is a Modal, with final words, and a confirmation button (the button in question). What happens is the New-UDElement -tag ‘div’ -Id ‘result’ gets the spinning progress indicator from the second UD-Dynamic , but never changes to the first one after the job code as finished.

It was my understanding that Jobs or tasks that may take time are to be put into a New-UDDynamic as to not stop the dashboard from loading, my thinking is that the Dynamic will internally wait on the task or job and then proceed to the next line under that same dynamic. And when loaded on a card that is true. While the button is different.

Thank you all for your time.

Tried some other things, decided to wrap the button itself into a New-UdDynamic, since I am still working under the impression that long running processes should be there. Came up with this that does work under that idea.

New-UDDashboard -Title 'PowerShell Web' -Content {
  
    New-UDElement -Tag 'div' -Id 'myElement' -Content {}

    New-UDdynamic -content {

        New-UDButton -Text 'Click Me' -OnClick {
        
            Set-UDElement -Id 'myElement' -Content {
                New-UDProgress -Circular -ProgressColor Red
            }

            start-sleep -Seconds 5
            Set-UDElement -Id 'myElement' -Content {
                New-UDTypography -Text "Complete" -Style @{ color = 'green' }
            }
        }
    }
}

I will integrate it into my original dashboard tomorrow. This is at least progress. :slight_smile:

**EDIT - Tested wonderfully, I marked this as solution.
And I want to thank @Jori for the initial explanation of how/why/when to use New-UDDynamic from another post I had.