Friday, April 19, 2024
HomeGolangRun Javascript ONCE - Getting Assist

Run Javascript ONCE – Getting Assist


The standard method could also be to place a code on each web page that examine if the javascript is loaded:

window.onload = perform () {
    if (localStorage.getItem("hasCodeRunBefore") === null) {
        //load code...
        localStorage.setItem("hasCodeRunBefore", true);
    }
}

Is it doable to name a “hidden” web page that solely runs ONCE no matter? Like pseudo code:

func init() {
	tpl = template.Should(template.ParseGlob("public/tmpl/*.html"))
    load code into website for each home windows (retailer in localStorage)?
}

With the sync package deal you possibly can run one thing just one time:

Instance:

package deal principal

import (
	"fmt"
	"sync"
)

func principal() {
	var as soon as sync.As soon as
	onceBody := func() {
		fmt.Println("Solely as soon as")
	}
	executed := make(chan bool)
	for i := 0; i < 10; i++ {
		go func() {
			as soon as.Do(onceBody)
			executed <- true
		}()
	}
	for i := 0; i < 10; i++ {
		<-done
	}
}

Thanks! However how do I run the Javascript and retailer in localStorage?

This subject was mechanically closed 90 days after the final reply. New replies are now not allowed.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments