I have an element which I want to be "clicked" every 5 seconds, does something, and then after 5 seconds it gets clicked again, does something, ect'.
So I used setTimeout, which does the job for the 5 seconds thing: (found the example here: How to hide a div after some time period?)
And there's the jQuery event .trigger which I found here: JQuery automatic click after 4 sec
I tried looping it using 'for' and I got a .click event that happens after the first 5 seconds, and then uses the .click event again and again without the 5 seconds pause: /
In general, I built my first jQuery rotator :)!
with right and left buttons - and I want it to move a slide after x seconds by clicking it "without clicking".
This is my .click event code for the "next" element button:
// variables
var rotCounter = 0;
var moveInPixles = 0;
var nunSlides = 4; // Enter the number of slides
$(".r-arrow").click(function(){
moveInPixles = ( rotCounter + 1) * 746 * -1;
moveInPixles += 'px';
$(".rotator-container").css('margin-left', moveInPixles);
rotCounter++;
$(".dot").css('background-color', 'white');
$(".dot:eq("+rotCounter+")").css('background-color', 'yellow');
if (rotCounter == nunSlides) {
rotCounter = 0;
$(".rotator-container").css('margin-left', 0);
$(".dot").css('background-color', 'white');
$(".dot:eq("+rotCounter+")").css('background-color', 'yellow');
}
});
I have an element which I want to be "clicked" every 5 seconds, does something, and then after 5 seconds it gets clicked again, does something, ect'.
So I used setTimeout, which does the job for the 5 seconds thing: (found the example here: How to hide a div after some time period?)
And there's the jQuery event .trigger which I found here: JQuery automatic click after 4 sec
I tried looping it using 'for' and I got a .click event that happens after the first 5 seconds, and then uses the .click event again and again without the 5 seconds pause: http://jsfiddle/WTJgt/417/
In general, I built my first jQuery rotator :)!
with right and left buttons - and I want it to move a slide after x seconds by clicking it "without clicking".
This is my .click event code for the "next" element button:
// variables
var rotCounter = 0;
var moveInPixles = 0;
var nunSlides = 4; // Enter the number of slides
$(".r-arrow").click(function(){
moveInPixles = ( rotCounter + 1) * 746 * -1;
moveInPixles += 'px';
$(".rotator-container").css('margin-left', moveInPixles);
rotCounter++;
$(".dot").css('background-color', 'white');
$(".dot:eq("+rotCounter+")").css('background-color', 'yellow');
if (rotCounter == nunSlides) {
rotCounter = 0;
$(".rotator-container").css('margin-left', 0);
$(".dot").css('background-color', 'white');
$(".dot:eq("+rotCounter+")").css('background-color', 'yellow');
}
});
Share
Improve this question
edited May 23, 2017 at 11:59
CommunityBot
11 silver badge
asked Jun 28, 2015 at 10:13
ImnotapotatoImnotapotato
5,87814 gold badges86 silver badges156 bronze badges
5 Answers
Reset to default 3setInterval(function () {
$(".r-arrow").click();
}, 5000);
Edit:
Use setInterval
and clearInterval
so that you can reset the interval on button click. Your code needs refactoring because you can't really use it as it is.
var intervalId;
var rotCounter = 0;
var moveInPixles = 0;
var nunSlides = 4; // Enter the number of slides
autoRotate();
function autoRotate() {
intervalId = window.setInterval(function () {
rotateStuff();
}, 5000);
}
function rotateStuff() {
moveInPixles = (rotCounter + 1) * 746 * -1;
moveInPixles += 'px';
$(".rotator-container").css('margin-left', moveInPixles);
rotCounter++;
$(".dot").css('background-color', 'white');
$(".dot:eq(" + rotCounter + ")").css('background-color', 'yellow');
if (rotCounter == nunSlides) {
rotCounter = 0;
$(".rotator-container").css('margin-left', 0);
$(".dot").css('background-color', 'white');
$(".dot:eq(" + rotCounter + ")").css('background-color', 'yellow');
}
}
$('.r-arrow').on('click', function () {
window.clearInterval(intervalId);
rotateStuff();
autoRotate();
});
Dont do a click, but just trigger the javascript function every five seconds. by triggering a click you also activate all other functions listening to the event and some might be in conflict with your code or used by other libraries.
You can use setInterval() to do so.
Your code should looks like this :
// variables
var rotCounter = 0;
var moveInPixles = 0;
var nunSlides = 4; // Enter the number of slides
var myFunction = function(){
moveInPixles = ( rotCounter + 1) * 746 * -1;
moveInPixles += 'px';
$(".rotator-container").css('margin-left', moveInPixles);
rotCounter++;
$(".dot").css('background-color', 'white');
$(".dot:eq("+rotCounter+")").css('background-color', 'yellow');
if (rotCounter == nunSlides) {
rotCounter = 0;
$(".rotator-container").css('margin-left', 0);
$(".dot").css('background-color', 'white');
$(".dot:eq("+rotCounter+")").css('background-color', 'yellow');
}
});
$(".r-arrow").click(myFunction);
setInterval(myFunction, 5000);
It is important to assign setInterval
to a variable then you will be able to stop it using clearInterval()
var intervalID = window.setInterval(code, delay);
Then when you need to stop it :
window.clearInterval(intervalID)
Would do the trick
var rightArrowIntervalId = setInterval(function(){
$(".r-arrow").trigger('click');
}, 5000);
In your click
event add at
$(".r-arrow").click(function(){
window.clearInterval(rightArrowIntervalId);
// will destroy existing interval
moveInPixles = ( rotCounter + 1) * 746 * -1;
.....
// will create new interval in 5 seconds
rightArrowIntervalId = setInterval(function(){
$(".r-arrow").trigger('click');
}, 5000);
});
I don't fully understand the question, but if you want to call a function every 5 seconds rather than just once then use setInterval().
Use setInterval function instead of setimeout
$( function() {
$('#login').click(function() {
alert('login button clicked');
});
setInterval(function() {
$('#login').trigger('click');
}, 500);
});
check updated JSFIDDLE
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744772093a4592820.html
评论列表(0条)