Skip to main content
Create new
Introduction
Reactivity
Props
Logic
Events
Bindings
Lifecycle
Stores
Motion
Transitions
Animations
Easing
SVG
Actions
Classes
Component composition
Context API
Special elements
Module context
Debugging
7GUIs
Miscellaneous
App.svelte
<script>
// import declarations
import { onMount } from 'svelte';
// variable declarations
let money = 20;
let numberOfBuilding = 0;
let buildingProduction = 1; // $ produced per building per tick
let tickSpeed = 1000;
// reactive declarations
$: cantBuy = cost > money;
$: cost = (numberOfBuilding + 1) * 5;
$: productionPerTick = numberOfBuilding * buildingProduction;
// function declarations
// update the values of `money` and `numberBuildings`
function updateNumbers(){
money -= cost;
numberOfBuilding += 1
}
// update `money` with `productionPerTick` and set a timeout to call itself after `tickSpeed` ms
function updateMoney(){
money += productionPerTick;
setTimeout(updateMoney, tickSpeed);
}
// lifecycle functions
onMount(() => {
updateMoney();
});
</script>

<style>
loading Svelte compiler...
/* Select a component to see its compiled code */
result = svelte.compile(source, {
generate: ,
});
/* Select a component to see its compiled code */