kit icon indicating copy to clipboard operation
kit copied to clipboard

goto doesn't work in setTimeOut

Open RohanNikumbh27 opened this issue 1 year ago • 1 comments

Describe the bug

if(isUserAlreadyExist){
	setTimeout(() => {
		goto("/dashboard/dashboard/asset");										
	}, 4000);
}else{
	goto("/auth/sign-up/successful");
}

In above code, after succeeding in 'if' condition i am trying to call 'goto' function after 4 seconds but it is getting executed immedietly instead of waiting for 4 seconds.

Reproduction

Needs to be changed in svelte and sveltekit's code.

Logs

No response

System Info

system: windows

Severity

annoyance

Additional Information

No response

RohanNikumbh27 avatar Aug 23 '24 13:08 RohanNikumbh27

"Reproduction" is supposed to contain the entire, self-contained code required to reproduce the issue. I also cannot reproduce the problem, goto works just fine in a setTimeout callback.

Entire page code:

<script>
  import { goto } from '$app/navigation';

  function onClick() {
    setTimeout(() => {
      goto('/a');
    }, 3000);
  }
</script>

<button on:click={onClick}>Go to /a</button>

Maybe you are calling it on the server? goto is for usage on the page, on the server you need to use redirect (which will not work in a regular timeout since at that point the response will have been sent, you would have to await a promise).

brunnerh avatar Aug 23 '24 14:08 brunnerh

FWIW, I used goto() within a setTimeout() recently for debugging and it worked. Although mine was not within an onclick handler.

"svelte": "^5.0.0-next.123", // (using runes mode)
"@sveltejs/kit": "^2.5.25",
"@sveltejs/adapter-cloudflare": "^4.7.2",

jasongitmail avatar Sep 04 '24 21:09 jasongitmail

This issue has been dead for a while. If you can still reproduce this issue, please:

  • npx sv create my-repro
  • git init && git add --all && git commit -m "Initial commit"
  • Add the code that minimally illustrates your issue
  • git add --all && git commit -m "Reproduce issue"
  • Create a repository on GutHub and upload your code
  • Open a new issue with the reproduction