How do I use javascript setTimout?

I have a small piece of javascript that I would like to use, but it takes a delay to activate it.

function sglClick(url) {
    window.setTimeout('location.href="http://'"+url",1500);
}

      

It doesn't work and I am really stuck here.


This is what I have now

function send(){

}

function sglClick(url) {
//    window.location.href="http://"+url;
    setTimeout(function send() { location.href = "http://" + url; },1500);
}

      

+1


a source to share


1 answer


try it

function sglClick(url) {
    setTimeout(function() { location.href = "http://" + url; },1500);
}

      

It is using an anonymous function to host your code. It takes about 1.5 seconds to fire. I say something like this because JavaScript is single threaded and other function calls / etc can actually do interval fire at slightly different times (but 9/10 is not a problem)

It is also assumed that you are sending things like this to a function



sglClick('www.google.com');

      

How does your code prefix the argument with http: //

I've always used this function as just setTimeout (), not as a method of the window object.

+6


a source







All Articles