Skip to content

Instantly share code, notes, and snippets.

@KartikTalwar
Created April 5, 2012 00:12
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save KartikTalwar/2306741 to your computer and use it in GitHub Desktop.
Save KartikTalwar/2306741 to your computer and use it in GitHub Desktop.
Make a JavaScript function repeat every x seconds
function refreshData()
{
x = 5; // 5 Seconds
// Do your thing here
setTimeout(refreshData, x*1000);
}
refreshData(); // execute function
@lazerg
Copy link

lazerg commented Aug 8, 2019

What if we want to send some arguments?

@AdiPP
Copy link

AdiPP commented Dec 10, 2019

What if we want to send some arguments?

U can use some ajax on '// Do your thing here' comment to passing some value or argument.
Btw, thanks for sharing this function.

@andorrandeded
Copy link

thank you bro

@jrojoCI3
Copy link

You can always set the call to your function inside an anonymous one:

setTimeout( () =>{
    console.log("performing next step");
    refreshData(param1, param2);
}, refresh_time);```

@arezo640
Copy link

arezo640 commented Dec 6, 2020

`document.addEventListener('DOMContentLoaded',motion)

//function
let i=0
,j=0;
function motion(){
let text2=" Find the lowest new and used prices on the books you need."
let text1="We make buying textbooks easy and affordable.<br> arezooo"

  if(text1.charAt(i)!="."){
    motionId1.innerHTML+=text1.charAt(i)
    setTimeout(motion,100)
    i++;   } 

else{
motionId2.innerHTML+=text2.charAt(j)
setTimeout(motion,100)
j++;
}
}
`

I have this code , but I want to keep text while text is typing on page... I think I need to keep calling function, but I don`t know it is possible. could you help me about that?

@jenykIV
Copy link

jenykIV commented Feb 2, 2021

how can i add document.getElementById("myId").innerHTML=myValue;?
if i have added this into this function, i has stopped working. Can someone help me?

@ymaallem
Copy link

ymaallem commented Feb 2, 2021

how can i add document.getElementById("myId").innerHTML=myValue;?
if i have added this into this function, i has stopped working. Can someone help me?

You have to create a variable outside your function and pass it as a parameter and then update it inside the function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment