如何在页面加载后禁用指定JavaScript函数20秒(WordPress场景)
Got it, here's a solid approach to temporarily disable your target function from leadclick.js for 20 seconds once the page finishes loading in your WordPress setup:
Step 1: Update Your PHP Script Enqueue Code
We'll add an inline script that runs right after leadclick.js loads. This script will override your target function with a "do nothing" version, then restore the original function after 20 seconds.
Modify your existing PHP code like this:
$LeAdClickonline = get_option('video-LeAdClick-online'); if ($LeAdClickonline === 'on') { wp_enqueue_script( 'video_LeAdClick_localized', plugin_dir_url(__FILE__) . '/js/leadclick.js', array('jquery') ); // Inline script to handle the 20-second disable wp_add_inline_script( 'video_LeAdClick_localized', ' // Replace "yourTargetFunction" with the actual function name from leadclick.js const originalLeadClickFunction = window.yourTargetFunction; // Override the function to do nothing temporarily window.yourTargetFunction = function() { // Optional: Add a console log or user message here console.log("This function is disabled for 20 seconds."); }; // Restore the original function after 20000ms (20 seconds) setTimeout(() => { window.yourTargetFunction = originalLeadClickFunction; console.log("Function is now active again."); }, 20000); ', 'after' // Critical: Ensures this runs AFTER leadclick.js loads ); }
Key Things to Adjust:
- Function Name: Replace
yourTargetFunctionwith the exact name of the function inleadclick.jsyou want to disable. For example, if the function is calledtrackLeadClick, usewindow.trackLeadClick. - User Feedback: The console logs are optional—you can replace them with a frontend alert (like
alert("Please wait 20 seconds before using this feature.")) if you want users to see a message.
Alternative: Edit leadclick.js Directly
If you'd rather modify the original script instead of adding inline code, wrap your function definition in a setTimeout inside leadclick.js:
// Inside leadclick.js setTimeout(() => { // Your original function code here window.yourTargetFunction = function() { // All the original logic from the function }; }, 20000);
This way, the function won't exist until 20 seconds after the script loads, so it can't be called during that time.
内容的提问来源于stack exchange,提问作者user9860866

