I'm getting alerted "hi" over and over again, how do I get it to do it once and stop:
function doSomething() {
alert('hi');
}
$(function() {
doSomething();
});
// Fired once when document is ready
$(document).one('ready', function () {
doSomething();
});
Using .one ensures this is done only once and not repeatedly.It is okay to put several document.ready event listeners (if you need other events to execute multiple times) as long as you do not overdo it, for the sake of readability.
.one is especially useful when you want the alert to appear the first time a web page is opened or when a mobile application is installed the first time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With