Trying to get the code to automatically change page using setTimeout, but I do not get it to work.
setTimeout()(page3, 500);
function page3() {
changepage3('automatic')
}
This is what my code looks like right now, but I am suspecting that this is not enough. Anyone knows what is missing?
try this one
function page3() {
changepage3('automatic')
}
setTimeout(page3, 500);
setTimout needs a specific syntax to work, check it out on the best JavaScript documentation by Mozilla: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Syntax
Here is an example
saySomethingAfter(5);
function saySomethingAfter(second) {
setTimeout(saySomething, second * 1000);
}
function saySomething() {
console.log("Something");
}
Your question is "How can I automatically change a page in Javascript?" using setTimeout. Let's analyse the needs:
change a page → open a new URL (cf. Open URL in same window and in same tab)
automatically using setTimeout → with the correct syntax
function changePage(url) {
window.open(url, "_self");
}
function changePageAfter5sec(url) {
setTimeout(function() {
changePage(url)
}, 5000);
}
changePageAfter5sec("https://stackoverflow.com")
Another way using beautiful functional JavaScript:
function changePage(url) {
return () => {
window.open(url, "_self");
}
}
function changePageAfter(second) {
return (url) => {
setTimeout(changePage(url), second*1000);
}
}
const changePageAfter5sec = changePageAfter(5);
changePageAfter5sec("https://stackoverflow.com")
You have 2 major problems in the code snippet provided:
That is not correct setTimeout() syntax - thus it doesn't actually work.
Even if it did work, it would call 1 function that uses another function which doesn't exist thus breaking the code.
fix problem number 1:
window.setTimeout(changePage, 5000);
now we have a running timeout that will trigger 5000 milliseconds after initiation(usually).
so let's fix problem 2 and let changepage() call an actual proper url opening function:
function changePage(){
window.open(urlOfPage3);
}
Finally a simpler version with an anonymous callback function in the setTimeout:
window.setTimeout(function(){
window.open(urlOfPage3);
}, 5000);
Related
function test() {
console.log("Hi");
setTimeout(function() {
location.reload();
console.log("Hi2");
}, 1000);
}
test();
Hi everyone i wonder if there is any chance that i can continue function/save progress of a function? What do i exactly mean with that? Well look at the code above. As you can see i have simple console.log, after it i setup a timeout for 1 sec, and i am reloading a page. I'ts clear that the console.log after page reload won't work, and here is my question. Is it possible to run that console.log after page reload, without triggering this first console.log?
Here is one way, I would have done it: using localStorage
localStorage.messages = []; //define an array to store all your messages
localStroage.messages.push('Hi');
localStroage.messages.push('Hi 2');
// don't call that line more than once, otherwise it will wipe out existing data
function test() {
check_messages();
setTimeout(function() {
location.reload();
check_messages();
}, 1000);
}
function check_messages() {
if (localStorage.messages.length>0) {
alert(localStorage.messages[0]); //display the message
localStorage.messages.splice(0,1); //get rid of it
}
}
test();
When using JavaScript, how do I create code that automatically clicks 2 links and opens it in a new tab?
If there is a link like, https://www.google.com/ how do I make it so that the code clicks the link every 3 minutes in a infinite loop?
Mine only clicks one link how do make it so that it changes it to open google.com and bing.com?
var i = 0;
function myLoop() {
setTimeout(function () {
window.open("http://www.google.com");
i++;
if (i < 20) {
myLoop();
}
}, 180000)
}
myLoop();
This was my code.
But, it does not work.
Following your question what you seem to be searching for is this:
function myLoop() {
window.open("http://www.google.com", "_blank");
}
window.setInterval(myLoop, 3*60*1000);
I advise you to have a look at this MDN documentation.
Now, about your code, you made some simple mistakes, that I believe will be cleared out after you the read MDN documentation (above), but just in case, I'll give you some help on that:
var i = 0;
// your function sets a timeout
function myLoop() {
// here
setTimeout(function () {
// and here it opens the link on the new tab, almost, you forgot the "_blank"
window.open("http://www.google.com");
i++;
// but this code bellow also calls myLoop, which will set a timeout
// again, as long as 'i' is lower than 20.
if (i < 20) {
myLoop();
}
}, 180000)
}
// this code calls your function (obviously)
myLoop();
Update
I used window.setTimeout instead of window.setInterval by mistake.
Please see my updated answer.
By looking at your code, I think that what you were trying to achieve is the same logic that is implemented by window.setInterval.
Although window.setInterval and window.setTimeout seem similar, the first calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function,
while the second, only calls that function once after the specified time delay.
I want to to check if the s.t() or page load has already been called on a site. I control when it gets called, but I want a very generic way to ask if it has already been called. The main purpose is to either call s.t() or s.tl() depending on what has previously happened.
This will return true if the SiteCatalyst code has fired.
(function(){for(w_m in window)if(w_m.substring(0,4)=='s_i_'&&window[w_m].src)if(window[w_m].src.indexOf('/b/ss/')>=0)return!0;})()
Unfortunately I do not know when this was introduced to AppMeasurement and I did not find any documentation about it but I accidentially found the following two callback functions that we use successfully to identify the moment shortly before and after the tracking request.
s.registerPreTrackCallback(function() {
console.log('Pre-Track');
});
s.registerPostTrackCallback(function() {
console.log('Post-Track');
});
This answer gives the concept behind it- before firing either function, check if the other was already fired. For example:
s.pageName="page";
s.eVar1="value";
if(!linkFired) {
var pageFired=true;
s.t();
}
if(!pageFired) {
var linkFired=true;
s.tl(this,'o','custom link');
}
I'm not sure this will actually give you any answers but you could also overwrite the s.t function with something like:
s.AltSt = s.t;
s.t = function (vo) {
s.AltSt(vo);
console.log("Do your own stuff!");
}
Haven't 100% tested this but on first observation this should work..
you could wait to see if s is called
setTimeout(function checkIfsLoad() {
if (typeof s == 'object') {
doYourStuff();
} else {
setTimeout(checkIfsLoad,150);
}
}, 150);
I am trying this code:
function view_mail_popup_close()
{
setTimeout("function () { $('#popupbox').fadeOut('slow'); }",200);
setTimeout("function () { window.location='view_mail.php' }",800);
}
I want to execute it onclick of a link, but only fading function works!
Can someone tell me why my redirect function not working?
I'm kind of surprised that either of them works, because you're giving setTimeout a string that defines a function without calling it; if you give setTimeout a string, it essentially does an eval on the string when the timeout occurs, which in theory would create but not call the function. (Edit: And I've confirmed that: http://jsbin.com/uvuje5)
It's almost never correct or necessary to give setTimeout a string; instead, give it a function:
function view_mail_popup_close()
{
setTimeout(function () { $('#popupbox').fadeOut('slow'); },200);
setTimeout(function () { window.location='view_mail.php'; },800);
}
Live example
There, the function is created immediately and the reference to it is given to setTimeout, which will call it when the timeout occurs.
(Off-topic: I've also added a missing semicolon at the end of the window.location = statemenet. JavaScript has semicolon insertion, and so the previous version would work, but I strongly advocate never relying on it.)
Update: As Capsule points out, there's a callback on fadeOut that you probably want to use instead of a second setTimeout:
function view_mail_popup_close()
{
setTimeout(function () {
$('#popupbox').fadeOut('slow', function() {
window.location='view_mail.php';
});
}, 200);
}
Live example
You should not be putting "function(){}" in quotes - if you use quotes then put the JS code directly there. What you are doing is syntactically incorrect:
Uncaught SyntaxError: Unexpected token
(
Just kill the quotes and feed function literals. You can nest them as such:
function view_mail_popup_close()
{
setTimeout(function () {
$('#popupbox').fadeOut('slow');
setTimeout(function () { window.location.href='view_mail.php' },600);
},200);
}
If you use quotes, it's slower because it does extra evaluation, and the scope is not kept intact because its defined in global scope, in addition you have to have the DIRECT JS code in there.
If this still doesn't make the page redirect to view_mail.php, please tell us specifically, exactly what happens. If it redirects to a 404/empty page, then you may need to specify root relative, eg href="/view-mail.php" with the leading /.
I have a small piece of javascript that I would like to use but need a delay before it's activated.
function sglClick(url) {
window.setTimeout('location.href="http://'"+url",1500);
}
This is not working and I'm really stuck here.
Here is what I now have
function send(){
}
function sglClick(url) {
// window.location.href="http://"+url;
setTimeout(function send() { location.href = "http://" + url; },1500);
}
try this
function sglClick(url) {
setTimeout(function() { location.href = "http://" + url; },1500);
}
This is using an anonymous function to house your code. It should take approx 1.5 seconds to fire. I say approx because JavaScript is single threaded, and other function calls/etc can actually make the interval fire at a slightly different time (but 9/10 this is a non-issue)
It is also assuming you're sending things like this to the function
sglClick('www.google.com');
As your code is prefixing the argument with http://
I've always used this function as just simply setTimeout(), not as a method of the window object.