Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Remove Component after time itself from DOM

Hello Guy's so I've created in Svelte a Component, which should itself remove after a time.

    <script>
    export let time;
</script>

<div class="notif">
  </div>

<style>
    
    
</style>

How would I archive that?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

From https://github.com/sveltejs/svelte/issues/3089

The difference in Svelte 3 is that component's can't self-destruct — there's no equivalent of this.destroy(). If a component can't destroy itself, that implies that some parent or controller must destroy it, ...

To destroy the component either an {#if} block could be used or calling component.$destroy()

A REPL with the examples

<script>
    import Comp1 from './Comp1.svelte'
    import Comp2 from './Comp2.svelte'

    let showComp = true

    let compRef
</script>

{#if showComp}
<Comp1 bind:showComp time={1500} />
{/if}

<Comp2 bind:this={compRef} self={compRef} time={1500}/>
Comp1
<script>
    import {onDestroy} from 'svelte'

    export let showComp, time

    setTimeout(() => {
        showComp = false
    }, time)

    onDestroy(() => {
        console.log('successfully destroyed Comp1')
    })

</script>

COMP 1
Comp2
<script>
    import {onDestroy} from 'svelte'
    
    export let self, time

    setTimeout(() => {
        self.$destroy()
    }, time)

    onDestroy(() => {
        console.log('successfully destroyed Comp2')
    })
</script>

COMP 2
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!