Vadim Tatarnikov Vadim Tatarnikov - 4 months ago 9
Javascript Question

Function only called once from $(function(){

So in my js script I use jQuery, at the top I wrote:

$(function() {
myFunc();
function myFunc() {
console.log("1");
}
});


"1"
is only printed once which means
myFunc
only ran once, I want it to run every frame/millisecond or basically as fast as it can over and over and over again. Why doesn't it happen like so? If I'm doing it wrong, how can I achieve the effect I want, and what is my mistake?

Answer

@Vadim Tatarnikov to call as soon as faster a function in jquery use window.setInterval() with minimum time interval try the below code

<script type="text/javascript" src="jquery.js"></script>//add your jquery script file 

<script type="text/javascript">
$(document).ready(function(){ 
window.setInterval(function(){
 myFunc();
},1);//here i put time interval=1 nanosecond
});
function myFunc(){
    console.log("1");
}

This will call myFunc() in every 1 nanosecond just run and see the console.