0

This line of code works on jsfiddle

var skipButton = document.getElementById("skip");
var counter = 10;
var newElement = document.createElement("p");
newElement.innerHTML = "Skip this after 10 seconds";
var id;

skipButton.parentNode.replaceChild(newElement, skipButton);

id = setInterval(function(){
counter--;
    if(counter < 0){
        newElement.parentNode.replaceChild(skipButton, newElement);
        clearInterval(id);
    } else {
        newElement.innerHTML = "You can skip this in " + counter.toString() + "seconds.";
    }
 }, 1000);

But when i tried it on my wamp it doesn't work . Why is that . Can someone send some suggestion why it is not working.

  • 1
    Where is this code in the context of your document? In your jsFiddle it's set to execute in the document's `onload` handler – Phil Jul 25 '17 at 01:09
  • they're just in 1 folder namely : splashscreen.js, splashscreen.css, splashscreen.html . is that what you mean by context sir? –  Jul 25 '17 at 01:11
  • 1
    Your javascript needs to either occur after your markup OR needs to be wrapped in a function that waits for the DOM to be loaded. – Lee Wise Jul 25 '17 at 01:11
  • @LeeWise what do you mean by that sir? –  Jul 25 '17 at 01:11
  • No. Where is the ` – Phil Jul 25 '17 at 01:12
  • yeah my html is like this `````` –  Jul 25 '17 at 01:13
  • Since your script is being called in the head before your – Lee Wise Jul 25 '17 at 01:15
  • @LeeWise so i should do it like this ```var skipButton = document.getElementById("skip"); var counter = 10; var newElement = document.createElement("p"); newElement.innerHTML = "Skip this after 10 seconds"; var id; if(document.readyState ==="complete"){ skipButton.parentNode.replaceChild(newElement, skipButton); id = setInterval(function(){ counter--; if(counter < 0){ newElement.parentNode.replaceChild(skipButton, newElement); clearInterval(id); } else { newElement.innerHTML = "You can skip this in " + counter.toString() + "seconds."; } }, 1000); }``` –  Jul 25 '17 at 01:17
  • You need to put ALL of your current js inside that function. So in my previous comment you would put your code where it says '//Already loaded!' – Lee Wise Jul 25 '17 at 01:19
  • @LeeWise still not working :( –  Jul 25 '17 at 01:21
  • Can you open the console log and see if there are any errors – Lee Wise Jul 25 '17 at 01:24
  • @LeeWise No errors. –  Jul 25 '17 at 01:25
  • My mistake, try this: ```document.addEventListener("DOMContentLoaded", function(event) { // Your Code Here });``` – Lee Wise Jul 25 '17 at 02:37
  • i already solved it sir thank you . :) i just change my html instead on the head my js script i wrote it down on my body tag –  Jul 25 '17 at 03:05

0 Answers0