Check parameter in URL and trigger javascript function

Asked

Viewed 38 times

-2

Good people, my idea is to see if the [Bust] parameter in the URL equals "Success" and if you want to change the <a> but only for 5 seconds, after that time I remove the class (in this case disabled)..

<script>
var bust = location.search.split('bust=')[1];
if(bust==success){
        $(".click").addClass("disabled");
setInterval(function(){
    $(".click").removeClass("disabled");
}, 5000);       
    }
</script>

2 answers

0

Definition and Use The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).

The setInterval() method will continue calling the function until clearInterval() is called or the window is closed.

The ID value returned by setInterval() is used as parameter for the clearInterval method ().

Tip: 1000 ms = 1 second.

Tip: To perform a function only once, after a specified number of milliseconds, use the setTimeout method () .

var myVar;

Function myFunction() { myVar = setInterval(alertFunc, 3000); }

Function alertFunc() { Alert("Hello!"); }

(the setInterval() method will perform the function once every 1 second, like a digital clock):

var myVar = setInterval(myTimer, 1000);

Function myTimer() { var d = new Date(); var t = d.toLocaleTimeString(); Document.getElementById("demo"). innerHTML = t; }

0

Maybe this will help

const urlParams = new URLSearchParams(window.location.search);
const busted = urlParams.get('bust');

if(busted === 'success') {
   $('.click').addClass('disabled');
   setTimeout(() => {
      $('.click').removeClass('disabled');
   }, 5000)
}

We use the interface Urlsearchparams to be able to work with the url’s query string.

With it we can pass the url, which in case is location.search and we use the method get() to retrieve query string values.

After this the desired check is done, the class is added and the setTimeout to remove the class after five seconds.

In that case you will use the setTimeout() because if put setInterval() it will perform always within that interval, it is something continuous. Already the setTimeout() runs only once after the interval.

https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals

Browser other questions tagged

You are not signed in. Login or sign up in order to post.