-4

I have this:

I'm trying to load this js code, value.id corresponds to an element that is detected after loading page. But actually this code is running before the element is loaded and im having this error:

Cannot read property 'nodeType' of null

I want to add something like delay on the document.getElementById(value.id)

I tried this, but nothing:

    const area = setTimeout(document.getElementById(value.id), 4000);
    Count.on(area, counter => {
    ...

and:

function setDelay() {
  document.getElementById(value.id);
}
setTimeout(setDelay, 4000);
const area = setDelay
Count.on(area, counter => {
...

both giving same error, any idea??

K3ny1
  • 61
  • 1
  • 6
  • You can use `window.onload` to set a function to be called after the page loads... – brso05 Jun 21 '18 at 19:58
  • Also what is `value.id`? – brso05 Jun 21 '18 at 19:59
  • Thanks, how you recommend me to use that window.onload using the piece of code i submitted at the top of the post. Thanks!! – K3ny1 Jun 21 '18 at 19:59
  • all the code is working perfect, i have all the values declared. I only need to know how to set up the delay :D – K3ny1 Jun 21 '18 at 20:00
  • it depends...what is `value.id`? – brso05 Jun 21 '18 at 20:00
  • is `value` a global variable? – brso05 Jun 21 '18 at 20:01
  • value.id is a value that my customer will setup, not me. My customer will create a element, then add the value he wants. Then my script will take that value, and get the data using that value. That's why i have to add a delay. – K3ny1 Jun 21 '18 at 20:01
  • yes, we can say its a global variable. – K3ny1 Jun 21 '18 at 20:02
  • So u are dependent on user input? Then you don't need a delay you just need to tap into the event triggered by the user...? – brso05 Jun 21 '18 at 20:02
  • No, lets say i add some fields to my customer. And my customer can decide to activate the function on some element. He adds a id to that element. And my script takes that id, and executes that. If i run the code not on loading page, after loading page works perfect. But i need also to be capable to run that after loading page, because if there's some data saved in that field i want to read it. – K3ny1 Jun 21 '18 at 20:04
  • Use `value.onload` to set a function that is called when value is loaded, i-e, when your customer would setup the value element. – Wololo Jun 21 '18 at 20:08
  • Hey Saud thanks, that makes sense. Any example?? Thanks! – K3ny1 Jun 21 '18 at 20:15
  • `value.onload = function() { const area = document.getElementById(value.id); //... };` ... try something like that – Wololo Jun 21 '18 at 20:18

2 Answers2

0

You can put code inside one function and call it setDelay function

function setDelay() {
    const area = document.getElementById(value.id);
    Count.on(area, counter => {
    ...
}
setTimeout(setDelay, 4000);

setTimeout will not return anything to caller.

Vivek
  • 1,307
  • 11
  • 24
  • Hello thanks Vivek for you answer. Tried but still getting the error. Another idea? – K3ny1 Jun 21 '18 at 20:15
  • @K3ny1 You should check `value.id` value OR may be you can use `setDelay.bind(this, value.id)` and change `function setDelay(valuesId)` – Vivek Jun 21 '18 at 20:18
0

Thanks to all, i solved it with this:

$(document).ready(function(){
// your code
});
K3ny1
  • 61
  • 1
  • 6