how to call javascript function using time period ? - javascript

<script>
var refresh;
function refresh(timeoutPeriod){
refresh = setTimeout(function(){window.location.reload(true);},timeoutPeriod);
}
makeClientRequest('live','liveFeed','');
window.onload=refresh(5000);
<script>
I want to call this function every 5 sec. I try that way.but it didn't work.

Try setInterval instead of setTimeout
refresh = setInterval(function(){window.location.reload(true);},timeoutPeriod);
setTimeout will call the function only once after the specified time period. But setInterval will call the function on specified interval of time

Related

How to pass a parameters under onload function?

<body onload="startTime('<%= time %>')">
</body>
<script>
setTimeout(function startTime(time) {
alert(time);
}, 1000)
</script>
I have returned the "time" from the client side by input from another page. Then, the "startTime()" will be called each seconds, in which the "time" parameter will be given to the function.
How can I pass the "time" variable in the <script> in every seconds under the onload function.
For running it every second, you have to use setInterval instead of setTimeout.
You should introduce your startTime outside of setInterval.
Your '<%= time %>' should not be a string too, it should be like this <body onload="startTime(<%= time %>)">.
<body onload="startTime(1000)">
</body>
<script>
function startTime(time) {
setInterval(function() {
alert(time);
}, time)
}
</script>
If time is a string you need to parse your time to a number.
<body onload="startTime('1000')">
</body>
<script>
function startTime(time) {
setInterval(function() {
alert(time);
}, Number(time)) //parse time string to a number
}
</script>
Though your question isn't much clear, maybe you can use $('document').ready(function(){});
inside your script if you use jQuery.
Also if you don't want to use jQuery another approach might be self executing anonymous function like this
<script>
(function(time) {
//this function will we invoked immediately
console.log(time);
})("Pass params here");
</script>
setTimeout will run the function after the timer (in your case 1000ms) has run out. If you want it to continuously run every N seconds, you need to use setInterval instead.
MDN: https://developer.mozilla.org/en-US/docs/Web/API/setInterval
You can pass arguments to the callback function, by passing a 3rd (or more) argument to the setInterval. But it won't be updated, you need to build your own code around it, so you get the updated value.
What you have in your code right now will not work. You don't call the callback function directly. setTimeout starts immediately after the page loads. Your startTime function is anonymous, and is essentially this:
setTimeout((time) => {
alert(time);
}, 1000)
// alert will be run after 1 second on page load
Another recommendation is, to not run code from HTML strings. Keep JavaScript related code inside of JavaScript. You can query HTML Elements inside of JavaScript and depending on if they changed, you can start running the code.

How to make a js function run immediately and then on a 10sec interval?

It only runs after 10 seconds. I want it to run when the page loads and then on a 10 seconds interval. Hope someone can help me.
function getPrice(){
$("#ajax").load('somefile.php?sym=<?php echo $yahoosymbol;?> #ajax');
}
getPrice();
setTimeout(getPrice, 10000);
UPDATE:
I got it to work after putting the function into a <body onload="function()">
none of the other things worked.. I still wonder why?
Try this :
function getPrice(){
$("#ajax").load('your code...');
}
getPrice(); //for initial execution
setInterval(getPrice, 10000); //runs the function on 10sec. interval
The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds
whereas setInterval() method does the same thing but at specified intervals repeatedly

How to run a JavaScript function after x seconds

I am trying a JavaScript function after 10 second of body load. But it is showing immediatly after body load. I am not expert in JavaScript, so I am not able to find where is the problem. my code is below:
<script type="text/javascript">
window.onload=setInterval(div_show(), 10);
</script>
<script>
window.onload = function(){
//time is set in milliseconds
setTimeout(div_show, 10000)
};
</script>
You need to:
Assign a function to onload. setInterval returns an interval id, not a function
Pass a function to setInterval, div_show() will call the div_show function and pass its return value
Multiple your number of seconds by 1000. setInterval's second argument is accepts a number of milliseconds not seconds.
Such:
onload = function () {
setInterval(div_show, 10 * 1000);
}
Finally, if you want to run the function 10 seconds after the document loads, rather than every 10 seconds starting from when the document loads, use setTimeout instead of setInterval.
Wrap it inside a function.
<script type="text/javascript">
window.onload = function(){
setInterval(div_show, 10);
}
</script>
Also, if you're sure if you want to execute this function only once when the body loads, you might as well want to use setTimeout instead of setInterval. Like
<script type="text/javascript">
window.onload = function(){
setTimeout(div_show, 10);
}
</script>
If you want 10 it to execute after 10 seconds, you need to set the timer parameter to number of seconds * 1000
In your case, 10*1000
Either
setTimeout(div_show, 10*1000);
or
setTimeout(div_show, 10000);
This code will work. Just set your time in milliseconds and write your JS code on loadAfterTime function:
<script>
window.onload = function(){
setTimeout(loadAfterTime, 1000)
};
function loadAfterTime() {
// code you need to execute goes here.
}
</script>

How do I call window.setInterval correctly?

I'm testing some code and I assume the code below will run the 'somefunction()' every 2 seconds. However, it only runs once. Why is this?
$(document).ready(function () {window.setInterval(somefuntion(), 2000);});
Because you are not telling setInterval() to run the function somefuntion() every 2 seconds, you are calling somefuntion() and passing its return value as parameter to setInterval().
Try again with window.setInterval(somefunction, 2000);.

Jquery/Ajax call with timer

I have a php page that echos out rows from a database. I want to call it via jquery/ajax every 30 seconds. But I also want to be able to call the page at any time so that if I add a record via the form, once the form submits I want the page via called to ajax to update the results right away. Can anyone point me in the right direction or provide some base code so I can try to figure this out? Still very new to jquery/ajax.
If you want to set something on a timer, you can use JavaScript's setTimeout or setInterval methods:
setTimeout ( expression, timeout );
setInterval ( expression, interval );
Where expression is a function and timeout and interval are integers in milliseconds. setTimeout runs the timer once and runs the expression once whereas setInterval will run the expression every time the interval passes.
So in your case it would work something like this:
setInterval(function() {
//call $.ajax here
}, 5000); //5 seconds
As far as the Ajax goes, see jQuery's ajax() method. If you run an interval, there is nothing stopping you from calling the same ajax() from other places in your code.
If what you want is for an interval to run every 30 seconds until a user initiates a form submission...and then create a new interval after that, that is also possible:
setInterval() returns an integer which is the ID of the interval.
var id = setInterval(function() {
//call $.ajax here
}, 30000); // 30 seconds
If you store that ID in a variable, you can then call clearInterval(id) which will stop the progression.
Then you can reinstantiate the setInterval() call after you've completed your ajax form submission.

Categories