Introduce a pause using the raycaster function

When my raycaster intersects an object for 2 seconds, I want to update the object's texture.

I attempted to use the clock function, but I am unsure of how to properly implement it.

var clock = new THREE.Clock();
clock.autoStart = true;

var intersects = raycaster.intersectObjects( pickable, true );

if ( intersects.length > 0 ) {
    if (!clock.running) clock.start()
    
    if (clock.getElapsedTime() > 2) {
        if (intersects[0].object.name === "btn_meeting_01") {
            mesh_sphere.material.map = THREE.ImageUtils.loadTexture( "textures/DIVE_IMMO_00004.jpg");
        }
        if (intersects[0].object.name === "btn_lobby_01") {
            mesh_sphere.material.map = THREE.ImageUtils.loadTexture( "textures/DIVE_IMMO_00003.jpg");
        }
        if (intersects[0].object.name === "btn_restaurant_01") {
            mesh_sphere.material.map = THREE.ImageUtils.loadTexture( "textures/DIVE_IMMO_00002.jpg");
        }
        if (intersects[0].object.name === "btn_entrance_01") {
            mesh_sphere.material.map = THREE.ImageUtils.loadTexture( "textures/DIVE_IMMO_00001.jpg");
        }
        if (intersects[0].object.name === "btn_exterior") {
            mesh_sphere.material.map = THREE.ImageUtils.loadTexture( "textures/DIVE_IMMO_00000.jpg");
        };
    }
}

Could you please point out any issues with this code snippet?

Answer №1

I found a solution utilizing a boolean variable and a setTimeout function.
While I understand that there may be more efficient methods, this is the approach I took.
Here is the code snippet showcasing my implementation:

var loading = false
var timeout = null

function render() {
  var intersects = raycaster.intersectObjects( pickable, true )

  if ( intersects.length > 0 ) {
    updateTexture()
  } else {
    resetLoader()
  }

  // Render the scene
}

function resetLoader() {
  if( loading ) {
    clearTimeout( timeout )

    loading = false
  }
}

function updateTexture() {
  if( !loading ) {
    timeout = setTimeout( function() {
      // Update the texture here
    }, 2000 )

    loading = true
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Efficient State Management with React-Redux for Streamlined HTTP Requests

In the process of developing a React-Redux application, I have encountered a situation where I need to display a cancel button while a specific HTTP request is ongoing. Upon successful execution of the request, the UI should display the results. If the use ...

I'm experiencing some difficulties utilizing the return value from a function in Typescript

I am looking for a way to iterate through an array to check if a node has child nodes and whether it is compatible with the user's role. My initial idea was to use "for (let entry of someArray)" to access each node value in the array. However, the "s ...

Tips for adjusting the height of a fixed-size child element on the screen using only CSS and JavaScript restrictions

I am faced with a challenge involving two child elements of fixed size: <div class="parent"> <div class="static_child"> </div> <div class="static_child"> </div> </div> .parent { border: 1px solid black; dis ...

Activate animation upon scrolling to specific element using Material-UI

Currently building out a website using react and Material-UI, I am looking to enhance the user experience with some transitions. At the moment, I have a button set up to display a component, but I want it to show up when I scroll to that specific part of ...

Utilize JavaScript to trigger a function depending on the selected options from dropdown menus

I've been working on a fun little project for a web page where users can select two items from a drop-down menu and then click a button to play a sound corresponding to their selections. However, I'm facing some challenges with getting my JavaScr ...

What causes the scrollbar to not extend to the bottom when connected to another scrollbar via a JavaScript equation?

I have been working on a code that involves multiple scrollbars that are all linked together. When you move one scrollbar, the other two will move proportionally. However, due to differences in width, the scroller doesn't always reach the end of the s ...

Can you explain the distinction between these codes?

function Example() { this.display1 = function() { alert(1) } } Example.prototype.display2 = function() { alert(2) } var e = new Example e.display1() e.display2() display1 and display2 both have the ability to trigger an alert showing a number. Do yo ...

Difficulty with Horizontal Mousewheel Scrolling

Struggling to implement a horizontal scrolling feature (via mousewheel) for both containers in the code below. I want this feature to be easily applied to any future container creations as well. <body> <style> #container { display: flex ...

Is the parent node of the input element the input element itself?

When working with a DOM object obj of type <input>, I encountered an issue where attempting to obtain its parent node using obj.parentNode resulted in the same obj being returned. Is this behavior specific to <input> objects? What other types o ...

Fixing Timezone Issue in VueJs 3 with Axios POST Request

Having an issue with axios. In my application, I have an input field for datetime-local. When I select a date and time, the displayed time is correct (e.g., selecting 10:00 shows as 10:00), but when calling a function using axios.post, the request sends th ...

Tips for updating and using a map value array as state in React

One React state I am working with is as follows: const [tasksMap, setTasksMap] = useState(new Map()); This map is created using the following code: tasks.forEach((task) => { const key = `${week.year}-${week.weekNo}`; if (!tasksMap.has(key)) { ...

What could be causing the error message 'Why is 'push' property undefined in React Native?

Within my index.ios.js file, I have the following: renderScene(route, navigator){ return <route.component navigator={navigator} {...route.passProps}/> } as well as render() { return ( <Navigator initialRoute={{name: &a ...

How can I handle a 404 error if an object is not found in a JSON file?

Below is my code snippet where I check for the correct req.path and display specific text. However, I now need to show a 404 not found error message. I attempted placing it inside the for loop condition with break;, but it's not quite working as expe ...

A helpful tip for creating line breaks within a Vue JS v-for loop using CSS

I am looking to arrange the names in alphabetical order when a list item is clicked. My tools of choice are Vue.js and Flex Grid. The list item I am working with is called ListAll, When clicking on ListAll, I want to display all the records grouped by na ...

Utilize AngularJS to enable the forward and back buttons to fetch JSON data, but ensure that the data is only retrieved if the item meets

I have successfully implemented forward and back buttons for paging through data items in an array from a JSON file: Controller: dishControllers.controller('DrinkcardsController', ['$scope','$http','$routeParams', ...

Information on the Manufacturer of Devices Using React Native

Struggling to locate the device manufacturer information. Using the react-native-device-info library produces the following output. There seems to be an issue with handling promises. I need to store the device manufacturer value in a variable. const g ...

Troubleshooting: Jquery show effects not functioning as expected

Currently, I am facing an issue where I am attempting to display a fixed div using the show function of jQuery. Although the show function itself is working properly, when I try to include an effect from jQuery UI, it does not seem to work as expected. Bot ...

Sending HTML parameters to a PHP file

I have been trying to pass the parameters from the current HTML page to a PHP page using a form. In my header in the HTML, I currently have the following function: <script> function getURLParameter(name) { return decodeURIComponent((new Re ...

Encountering no automatic refresh feature in Next.js

Encountering an issue with Next.js where the local host index page doesn't automatically refresh whenever a change is made. To provide some context, I initiated a Next.js application using npx create-next-app --use-npm. After starting the local serve ...

What is the best way to extract data from an array of objects in a JSON response?

I am currently exploring the utilization of API's in React. Right now, I am developing a search input feature for country names using the Rest Countries API. The data is being pulled from . However, I am facing a challenge in handling this data as it ...