user5051092
user5051092

Reputation:

Timeout not happening - Simple Javascript

I'm trying to make a piece of code that will show up a message when nothing is entered into a text field, and then disappear (I may make it fade instead of simply disappear but lets walk before we can run...).

What I've done so far looks as so:

<div id="msg"></div>

<script>
var x = document.getElementById("msg");
    x.innerHTML = "Enter a video URL";
    setTimeout(function() {
        x.innerHTML = "");
    }, 1000);
</script>

it seems the timeout function isn't working as when I replace:

setTimeout(function() {
    x.innerHTML = "");
}, 1000);

with

setTimeout(function() {
    x.innerHTML = "A");
}, 1000);

The A instantly shows up, signifying the "enter a video" string isn't being executed and the browser is jumping straight to the string I want to be halted.

Upvotes: 0

Views: 41

Answers (3)

Abdelrahman Elkady
Abdelrahman Elkady

Reputation: 2576

The signature is setTimeout(func,delay)

So you simply need to fix your typo in the function like :

setTimeout(function() {
    x.innerHTML = "SOME_VALUE";
}, 1000);

Upvotes: 0

Hazonko
Hazonko

Reputation: 1035

Using this should do it. You had a couple errors..

setTimeout(function() {
    x.innerHTML = "A";
}, 1000);

edit: didn't notice it wasn't jQuery, so changed my response...

Upvotes: 0

nlgn
nlgn

Reputation: 388

You have a miss matched brackets in your timeout:

<div id="msg">start</div>

JS:

var x =  document.getElementById("msg");
x.innerHTML = "Enter a video URL";
setTimeout(function() {
          x.innerHTML = "end";
}, 1000);

Upvotes: 1

Related Questions