-2
<div id="c" onClick="func()"></div>

This will execute funct() only when the user will click. But I want to execute func() automatically after 10 seconds. Somethink like this:

<div id="c" After10Seconds="func()"></div>

1 Answers1

2

Write some logic. Use a flag and a timeout on the page load:

var hasCalledFunc = false;
setTimeout(function() {
    if (hasCalledFunc == false) func();
}, 10000);

function callFunc() {
    if (hasCalledFunc == false) func();
}

function func() {
    hasCalledFunc = true;
    //..rest of func
}

And the HTML:

<div id="c" onClick="callFunc()"></div>

Or, all in func

var hasCalledFunc = false;
function func() {
    if (hasCalledFunc) return;
    else hasCalledFunc = true

    //rest of func
}

setTimeout(func, 10000);
tymeJV
  • 103,943
  • 14
  • 161
  • 157