Calling Javascript code automatically after 2 seconds [duplicate] - javascript

This question already has answers here:
what is setTimeOut() function in javascript?
(5 answers)
Closed 3 years ago.
i want to run similar task for my site. I want to run javascript code when user visit or click my URL, the code should be run after 2 seconds automatically after loading my page.
Please help.

You can use below code
window.onload = function() {
setTimeout(function() {
your functions()
},2000);
}
Onload function is called after page load complete and setTimeout is used for delayation

Related

refresh div once (with jquery) [duplicate]

This question already has answers here:
Sleep in JavaScript - delay between actions
(15 answers)
Closed 7 years ago.
I want widget.Rotator.rotate() to be delayed 5 seconds between calls... how do I do this in jQuery... it seems like jQuery's delay() wouldn't work for this...
You can use plain javascript, this will call your_func once, after 5 seconds:
setTimeout(function() { your_func(); }, 5000);
If your function has no parameters and no explicit receiver you can call directly setTimeout(func, 5000)
There is also a plugin I've used once. It has oneTime and everyTime methods.
jQuery timers plugin
var rotator = function(){
widget.Rotator.rotate();
setTimeout(rotator,5000);
};
rotator();
Or:
setInterval(
function(){ widget.Rotator.rotate() },
5000
);
Or:
setInterval(
widget.Rotator.rotate.bind(widget.Rotator),
5000
);

How to disable a link (preventDefault()) for 10 seconds, after user clicks on it? [duplicate]

This question already has answers here:
Sleep in JavaScript - delay between actions
(15 answers)
Closed 7 years ago.
I want widget.Rotator.rotate() to be delayed 5 seconds between calls... how do I do this in jQuery... it seems like jQuery's delay() wouldn't work for this...
You can use plain javascript, this will call your_func once, after 5 seconds:
setTimeout(function() { your_func(); }, 5000);
If your function has no parameters and no explicit receiver you can call directly setTimeout(func, 5000)
There is also a plugin I've used once. It has oneTime and everyTime methods.
jQuery timers plugin
var rotator = function(){
widget.Rotator.rotate();
setTimeout(rotator,5000);
};
rotator();
Or:
setInterval(
function(){ widget.Rotator.rotate() },
5000
);
Or:
setInterval(
widget.Rotator.rotate.bind(widget.Rotator),
5000
);

how to execute a function after the page is loading is done in angularjs [duplicate]

This question already has answers here:
How to run function in AngularJS controller on document ready?
(10 answers)
Closed 6 years ago.
I have an angularjs app. In the html page I call some REST services when the page is loading. I want to calculate the time taken in loading the page.
I found below code at Calculating Page Load Time In JavaScript:
window.onload = function () {
var loadTime = window.performance.timing.domContentLoadedEventEnd-window.performance.timing.navigationStart;
console.log('Page load time is '+ loadTime);
}
But it uses window.onload.
Which angularjs directive should I use to execute this logic after the page load is done.
Thanks.
angular.element(document).ready(function(){
//do it here
});

How to change current page in Phantomjs using buttons? [duplicate]

This question already has an answer here:
Open link in PhantomJS synchronously
(1 answer)
Closed 6 years ago.
I have start to using Phantomjs and I don't understand something. How to change current page using buttons? For example I've got a code:
var page = require('webpage').create();
page.open('https://ru-ru.facebook.com', function() {
page.injectJs('jQuery.js');
page.evaluate(function() {
$('#email').val('MyLogin');
$('#pass').val('MyPass');
$('#u_0_l').click();
});
page.render('example.png');
phantom.exit();
});
So after clicking a button I need to go to the next page. How can I do this?
Assuming the $.click() actually worked, you need to wait until the page has loaded.
setTimeout:
You may feel lucky and try it with
page.evaluate(function() {
// form vals and click
});
setTimeout(function(){
page.render('example.png');
phantom.exit();
}, 3000); // 3 sec timeout
waitFor selector:
You may use the waitFor example from the phantomjs package to wait for an element that you know is only on the loaded page, but not on the current page.
waitFor loadFinished:
You may combine waitFor with the onLoadFinished callback handler to do wait for the next page to load. This is preferable since this would work every time and would not impose overshooting with a conservative timeout.
Just use CasperJS:
Nothing more to add.

how to load a .each function on body load [duplicate]

This question already has answers here:
Trigger event on body load complete js/jquery
(10 answers)
Closed 8 years ago.
based on my previous question (HERE) ive updated my time ago code block, it works fine,the interval gets called every 10 sec but im faced with yet another silly problem . My problem is that i dont know how to load the function as soon as the body is loaded
my code (the important part)
$(".elapsed_time").each(function() {
time_r = $(this).data('time_raw');
var self = $(this);
var inter = function() {self.html(time_ago(time_r));}
setInterval(inter, 10000);
});
This can be done in jQuery by wrapping your code in the following:
$(document).ready(function () {
//-- This code will run when the body is loaded
});
You can also use the shorthand notation to accomplish the same thing:
$(function () {
//-- This code will run when the body is loaded
});
Edit: Kevin raises a good point, this is technically different from a true 'body load' event. The way I interpreted your issue, you wanted to wait until all of the elements are available before running your code.
More info on this: window.onload vs $(document).ready()

Categories