How can I solve this problem with the screen touches? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I would like you to tell me if there is an event that returns to me when it was the last time a user touched the screen, since I need a function to run 3 seconds after the user touched the screen for the last time

There's no specific event for your use-case. What you can do however is adding a click event listener to your window object and assign the current time to a global variable as soon as someone clicked the screen. Additionally you have to use JavaScript's setInterval() method, which executes a function periodically e.g. every 20 ms. Inside it's callback function you can compare the current time to the time stored in the global variable and if the difference is bigger than 3000 trigger your action.
Here's an example (just click 'Run code snippet'):
var timeClicked;
function screenClicked() {
timeClicked = new Date();
}
function check() {
if (timeClicked != null) {
if (new Date() - timeClicked > 3000) {
clearInterval(interval);
alert("time's up!");
}
}
}
window.addEventListener("click", screenClicked);
var interval = setInterval(check, 20);
<body bgcolor="#eeeeee"><span>click anywhere inside this window</span></body>

Related

How to run an event function once [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a lot of questions its all on java script (with some jquery)
first im trying to detect the mouse X cordinates when the mousemove on an element :
(function() {
'use strict';
$('.AnyElement').mousemove(function (e) {
console.log(e.pageX)
});
})();
i want to detect the mouse X once i know theres a functions like mouseover etc...
but in general how to make this function run once and stop
Second when someone write :
if (document.body = 1) {
// do anything
}
he is checking if document.body equal to 1
i see a thing in someone else code i dont undertand here it is :
if (document.body) {
// do anything
}
it doesnt matter what the function do , the thing is what he is checking ???
In answer to your first question there are a few ways you could do it, one example would be to register the mousemove event and then remove the event after logging it once.
$('html').mousemove(function(e) {
console.log(e.pageX);
$('html').off('mousemove');
})
Another method could be use the one event listener built into jQuery.
$('html').one('mousemove',function(e) {
console.log(e.pageX);
});
In answer to your second question the first statement is looking for the length of the element, if the element exists it will generally be greater than 0. In the second statement document.body will return a boolean of true or false depending on whether or not the element exists. Again there are a million different ways you can do the same thing in Javascript.
Hope that helps!

Complicated repeated function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Please Help me to solve this complicated loop. I’m calling 5 API URL but each of them should be called on a specific number of times then it should start to second, third, fourth and fifth URL and start again from top to bottom again and gain.
https://www.example1.com should be called 4 times
https://www.example2.com should be called 10 times
https://www.example3.com should be called 8 times
https://www.example4.com should be called 9 times
https://www.example5.com should be called 6 times
Should end on https://www.example5.com and again start from top https://www.example1.com
Unstoppable loop.
I highly Thanks & appreciate that anyone which answers this.
My code:
This is what I have tried so fo
The result of the code is commented inside the above code.
Use a variable as counter for each function like below,
var numberOfExecution=0;
function1(); // Start the procedure
function1()
{
// do api call
.......
// after finishing your task, check if this function execution hits desired number
numberOfExecution++;
if(numberOfExecution==4)
{
numberOfExecution=0;
function2();
}
else
{
function1();
}
}
function2()
{
// do api call
.......
// after finishing your task, check if this function execution hits desired number
numberOfExecution++;
if(numberOfExecution==6)
{
numberOfExecution=0;
function3();
}
else
{
function2();
}
}
In these process, one after another execution will continue achieving desired number of execution.

Can someone tell me why this javascript function does not execute? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm sure this is a very simple solution. I have made this javacript function that tests whether there is a certain css style on a div and then moves around another div. However, it does not work and I have no idea why.
JavaScript:
function sale() {
var style = document.getElementsByClassName("product-single__price--wrapper").getAttribute("style");
if (style !="display: none;") {
document.getElementByClassName("product-single__description").style.marginTop = "70px !important";
}
}
window.onload = sale;
I wouldn't ever suggest doing this, but if you want to call that function all the time, you need to put it into a setInterval with the milliseconds you want it to get called.
Example:
$(document).ready(function() {
setInterval(function() {
sale();
}, 1000);
});
OR
$(document).ready(function() {
setInterval(sale, 1000);
});
This will get called every second. Again, horrible horrible horrible practice. But, this will do what you want. If you want it called sooner, then change the milliseconds accordingly (1000 milliseconds = 1 second).

reset interval doesn't work in javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
clearInterval(interval)
var interval = setInterval(function(){
console.log('running')
},1000);
I have above code in a click event, the console.log('running') turn out to be trigger multiple times when I execute above code, why? I already clear the interval first before run the setInterval.
Assuming your code looks something like this:
$('#someButton').on('click', function() {
clearInterval(interval)
var interval = setInterval(function(){
console.log('running')
},1000);
});
then the problem you're having is all to do with scope. The second time your button is clicked, the function will run, but will have no reference to any variables created inside the function the first time it ran. (Try adding console.log(interval) as the first line in the click handler function). To solve this, you'll need to keep a reference to interval somewhere that the click handler can access it. For example:
var interval = null;
$('#someButton').on('click', function() {
clearInterval(interval)
interval = setInterval(function(){
console.log('running')
},1000);
});
See What is the scope of variables in JavaScript? for some examples of scope in action.
Ok, now I understand your problem. You need to declare interval in a higher scope (global if needed).
Your problem is the interval varible is declare inside the click function and therefor it's a local varible, you need to keep it elsewhere to access the reference in order to clear it.
Try this, and do not put all this in the click function, put it on the same level with the click function should work.
var interval;
function doInterval() {
if (interval != undefined) clearInterval(interval);
interval = setInterval(function(){
console.log('running');
},1000);
}
P/S: you should edit your question to clarify the question.

Determine order functions are called in [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
So I have a list of functions in JavaScript, each function has an associated button.
I want to know how you can tell in which order the buttons were pushed.
There's no native way for JavaScript to keep track of when a function was called, as this would have too much of a performance impact on the engine. You'll need to modify your code to keep track of this information internally. For example, you could use an array to log each call.
var log = []; // List of calls made in order
function one()
{
log.push('one'); // Log call
// ...
}
function two()
{
log.push('two'); // Log call
// ...
}
function three()
{
log.push('three'); // Log call
// ...
}
// Call in some order (which could be done by the user of course):
two();
one();
three();
You could of course easily reset your log as well:
log = []; // Reset

Categories

Resources