setOnClickAsync( onClickAsync: function() { // Fetch data from API (example: JSON placeholder) $url = 'https://jsonplaceholder.typicode.com/todos/1'; $context = stream_context_create([ 'http' => [ 'timeout' => 10, 'method' => 'GET', ] ]); $response = @file_get_contents($url, false, $context); if ($response === false) { throw new \Exception('Failed to fetch data'); } return json_decode($response, true); }, onComplete: function($data) use ($statusLabel, $resultLabel) { $statusLabel->setText('✓ Daten erfolgreich geladen!'); $formatted = "ID: " . ($data['id'] ?? 'N/A') . "\n"; $formatted .= "Titel: " . ($data['title'] ?? 'N/A') . "\n"; $formatted .= "Erledigt: " . (($data['completed'] ?? false) ? 'Ja' : 'Nein'); $resultLabel->setText($formatted); }, onError: function($error) use ($statusLabel, $resultLabel) { $statusLabel->setText('✗ Fehler aufgetreten'); $resultLabel->setText('Error: ' . $error->getMessage()); } ); // Button for simulating slow operation $slowButton = new Button( text: 'Langsame Operation (5s)', style: 'bg-orange-500 hover:bg-orange-700 text-white p-3 rounded-lg' ); $slowButton->setOnClickAsync( onClickAsync: function() { $startTime = microtime(true); // Simulate heavy computation sleep(5); $endTime = microtime(true); $duration = round($endTime - $startTime, 2); return [ 'message' => 'Operation abgeschlossen', 'duration' => $duration . ' Sekunden' ]; }, onComplete: function($result) use ($statusLabel, $resultLabel) { $statusLabel->setText('✓ Operation abgeschlossen!'); $resultLabel->setText( $result['message'] . "\n" . 'Dauer: ' . $result['duration'] ); }, onError: function($error) use ($statusLabel, $resultLabel) { $statusLabel->setText('✗ Fehler bei Operation'); $resultLabel->setText('Error: ' . $error->getMessage()); } ); // Add all components $container->addComponent($title); $container->addComponent($statusLabel); $container->addComponent($fetchButton); $container->addComponent($slowButton); $container->addComponent($resultLabel); // Info label $infoLabel = new Label( text: 'Die UI bleibt während der Requests responsive!', style: 'text-xs text-gray-500 mt-4 italic' ); $container->addComponent($infoLabel); // Run application $app->setRoot($container)->run();