-11

In my JavaScript file, I want to call a function 13 seconds after the page has loaded. How can I do this properly, without blocking anything? What I tried before caused the whole page to be stuck (frozen).


Well here is my code.

setTimeout(function(){startCalculation();},-13000);

function startCalculation() {
  var v = 0;
  var x = 0;

  var element = document.getElementById("price");
  if(typeof element !== "undefined") {
    element = window[v].innerHtml;
  }
}
Patsy Issa
  • 11,113
  • 4
  • 55
  • 74
Jase
  • 149
  • 14
  • 4
    Use setTimeout. Search for it. – Denys Séguret May 26 '15 at 18:11
  • If you had a bug while using it, please reproduce your problem and show us the relevant code. – Denys Séguret May 26 '15 at 18:13
  • Well, if you want to call teh function after 13 seconds, then it should not be -13000, just use 13000 (no minus) – The Guest May 26 '15 at 18:28
  • Based on your edit, it may appear to be "stuck" because `startCalculation()` isn't doing very much. It looks up the `price` element, then reassigns the variable to another elements's `innerHTML`, discarding the element and doing nothing more with the HTML. Perhaps you meant something like `element.value = window[v].innerHtml;`. – Jonathan Lonowski May 26 '15 at 18:29
  • You also need to wrap that in a window.onload=function(){...} – mplungjan May 27 '15 at 04:38

1 Answers1

0

You can use the setTimeout function that is built into JavaScript for this. It will not prevent the user from interacting with your page:

setTimeout(function()
{
    yourFunctionHere();
},
13000);
PIJNSO678
  • 86
  • 8
  • 3
    Which can be shortened in `setTimeout(yourFunctionHere, 13000)` – Denys Séguret May 26 '15 at 18:14
  • Yes, you can shorten it if you like. – PIJNSO678 May 26 '15 at 18:15
  • This is exactly the code I was using, @PIJNSO678! If this is the right code then wyj doesn't it work? Please help me, I don't understand. – Jase May 26 '15 at 18:16
  • you'd need to define "doesnt work" in your question – castis May 26 '15 at 18:17
  • @Jase If you post your code, then it would be helpful – The Guest May 26 '15 at 18:17
  • @castis I never said, "doesn't work". I actually told you exactly what happens. The whole page freezes when this code runs. – Jase May 26 '15 at 18:18
  • Why was my answer downvoted? It is the correct code. – PIJNSO678 May 26 '15 at 18:18
  • @TheGuest I will post my code then. I didn't think I should, because it was wrong and I thought it might cause confusion. – Jase May 26 '15 at 18:19
  • @Jase Please post the code you use to set the timeout and also the code of the function you are calling inside the timeout. – PIJNSO678 May 26 '15 at 18:20
  • I have updated my question to include the code I was using. – Jase May 26 '15 at 18:25
  • @Jase Ok. I can see your code. There are too many problems here. First, please explain your goal here. What do you want to do 13 seconds after the page loads, how do you intend on doing it? You cannot perform a task in the past (your code says -13000). What exactly are you trying to assign to the element `price`? – PIJNSO678 May 26 '15 at 18:30
  • Stuff you, why won't you help me?? You're just making me feel like crap just because you probably have lots of rep on this site you think you're smarter than me. I posted my code and asked for help and said why it wasn't working and now my codes intentions are being questioned? What the? – Jase May 26 '15 at 18:32
  • I'm sorry if I have made you feel bad. I am simply trying to understand what's going on here, so I can help you. And I don't think you've seen my profile. Because if you did, you'd see that you have about 70 rep more than me. Here's something that we can try: In your function called `startCalculation`, I want you to comment out everything and add 1 line of code: `alert("test");` and run the web page. Tell me what happens. – PIJNSO678 May 26 '15 at 18:37
  • @PIJNSO678 OK. You were right. Sorry! It was problem with my code in the startcalculation method. – Jase May 26 '15 at 18:49