$ not found error while using setinterval - javascript

I am trying to automatically click a button when it is created and after clicking the button, close the interval. But the following script is giving me $ is not defined error. Can anyone help me where am I doing wrong?
var timer = setInterval(
function () {
if ($('#element')) {
$('#element').click();
clearInterval(timer);
}
else
{
console.log('Element not found');
}
}, 1000);

only if you are using jQuery add their cdn in your html page
<script
src="https://code.jquery.com/jquery-2.2.4.js"
integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="
crossorigin="anonymous"></script>

Related

<div> value not updating in html page

I am using jquery to auto update a part of the HTML page. Following is the code
$(document).ready( function() {
$('#auto').load("static/l.txt");
refresh();
});
function refresh() {
setTimeout(function() {
$('#auto').load("static/l.txt");
refresh();
}, 1000);
}
The id of the HTML div tag to be updated is auto
The file static/l.txt is continuously being updated by another python program.
But when I load the html page , the div only gets updated once and does not update the value until and unless I open the developers console on the browser.
I am hosting the web page using Flask in python
How about trying this?
var counter =0 ;
$(document).ready( function() {
$('#auto').val("starting");
refresh();
});
function refresh() {
setInterval( function() {
counter ++;
$('#auto').val(counter);
}, 1000);
}
Use a callback to know, when the content is actually loaded and move the function definition in the ready block, also.
$(document).ready(function() {
function loadData() {
$('#auto').load('static/l.txt', function(completed) {
setTimeout(loadData, 1000);
});
}
loadData();
});

Why doesn't my page auto refresh properly?

My goal is to refresh the page when the class p.fancybox-error is visible, but I would like to know why this part of the code doesn't work at the top of my page.
<script type="text/JavaScript">
var theDiv = document.querySelector("p.fancybox-error");
theDiv.addEventListener("click", function() {
setTimeout(function(){ location.reload(); }, 5000);
});
</script>
I put it under this line:
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
But it gives me this error:
null is not an object (evaluating 'theDiv.addEventListener')
Thanks a lot guys, just take a look to #Stephane's answer, it works perfectly for me.
This polls every second looking for your class and auto-reloads when it exists
$(function() {
setInterval(function() {
if($("p.fancybox-error").length) location.reload();
}, 1000);
});

How do I correctly update setInterval using jquery/javascript?

So I have a div that fades in and out on the screen, and the rate at which this occurs is given by the value of an input which functions as a knob (by using knob.js). I want the user to be able to dynamically change the value on the knob, which will then set the new rate for setInterval. However, this is not happening, it continues to be stuck at the default value regardless of the change on the dial/knob. If anyone can point out what I'm doing wrong, or help in any way I would be very appreciative.
$(function (){
$(".dial").knob();
$("#reknob").val().trigger("change");
});
function fadeRed() {
$("#rediv").fadeOut();
$("#rediv").fadeIn();
}
function getRed() {
refreq=$("#reknob").val();
return refreq;
}
setInterval(function () {
fadeRed();
},getRed());
You need to use a change handler to reset the interval
$(function() {
var reknobInterval;
$("#reknob").knob({
change: function(v) {
reset(+v)
}
});
reset(+$("#reknob").val())
function reset(value) {
$("#rediv").stop(true, true).show();
clearInterval(reknobInterval);
reknobInterval = setInterval(fadeRed, value * 1000); //convert the delay to seconds
}
function fadeRed() {
$("#rediv").fadeOut().fadeIn();
}
});
<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js"></script>
<script type="text/javascript" src="http://anthonyterrien.com/js/jquery.knob.js"></script>
<input id="reknob" class="dial" data-width="150" data-cursor=true data-thickness=.3 data-fgColor="#222222" data-min="1" data-max="10" />
<div id="rediv">rediv</div>

Javascript refresh webpage every 5s

I refrence the JS unlock right click script:
javascript:(function() { if(window.addEventListener) window.addEventListener(a, function (e) { for(var n=e.originalTarget; n; n=n.parentNode) n[ona]=null; }, true); window[ona]=null; document[ona]=null; if(document.body) document.body[ona]=null; } R("contextmenu"); R("click"); R("mousedown"); R("mouseup"); R("selectstart");})()
when you put the link in the url and enter, you can use right.
so I want to make a js link to put in the url and enter, the website will refresh every 5s.
javascript:(function() {
function refresh() {
window.location.reload(true);}
setTimeout(refresh, 5000);
})()
but I can't do that, please help.
because my English too bad, if u don't understand, ask me.
<meta http-equiv="refresh" content="5; URL=http://www.yourdomain.com/yoursite.html">
If it has to be in the script use setTimeout like:
setTimeout(function(){
window.location.reload(1);
}, 5000);
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
// -->
</script>

Quit function on body onLoad

I need a script thats redirects the user to a second side, when the mainpage need to long to load.
I did this:
<script type='text/javascript'>
$(document).ready(function() {
setTimeout("location.replace('contact.html')",10000);
});
</script>
To sametime a preloader opens an get hided on on body onload, when that happens i need to kill the functon above?
Anybody an idea?
Try this:
var redirect = setTimeout(function(){
window.location = 'somewhere'
}, 10000)
$(document).ready(function() { // or $(window).load(function() {
clearTimeout(redirect);
});
setTimeout("location.replace('contact.html')",10000);
Instead, you can try
setTimeout(function(){window.location='contact.html'},10000);
You Can try Following
var Ispreloader=false;
<script type='text/javascript'>
$(document).ready(function() {
if(!Ispreloader)
{
myfunction();
}
});
function myfunction()
{
setTimeout("location.replace('contact.html')",10000);
}
//------------Whenever preploadeer loads on bodu unload set the variable to true
Ispreloader=true;
</script>

Categories