weixin_33743880 2015-01-06 01:04 采纳率: 0%
浏览 12

Javascript警报重复

I am writing a script to prevent people from being able to right click on an image. Once they click on the image I have set a Alert box to show to state that the images are copyrighted. To try and explain how this works, the image is in a model that doesnt appear in the DOM until another ID is clicked (done through AJAX). The script I have waits till the ID is clicked and the model loads, and then executes the function to show the Alert if there is a right click. The issue is, there are multiple buttons that can be clicked on to make the model appear, and so, in my code I targeted each of them, and if any of them are clicked, the function fires. The issue happens when you click the ID to make the model appear, close the model, and then click another ID to make the same model appear. When this is done, the alert shows multiple times. And so, if you spend some time clicking on all the IDs that fire the function, when you eventually right click the image, the alert may show 10-20 times. I am looking for a way to make the alter only show once, no matter how many times the function is executed.

My script is below:

function imageClickerz( ) {
    $( "#sb-wrapper-inner" ).each(function( index ) {
         $('#sb-wrapper-inner, #sb-nav-previous, #sb-nav-next').mousedown(function(event) { 
            switch (event.which) {
            case 3:
                    alert('Sorry, our images are copyrighted.');
                    break;
            }
        });
    });
}
$("#product_photo").hover(function(){
  setTimeout(function (){
       $('div[id^=vZoomMagnifier], img[id^=alternate_product_photo_], .vCSS_img_product_photo, .vCSS_img_larger_photo, #product_photo, #sb-nav-previous, #sb-nav-next, ').click(function(event){
        setTimeout(function (){
          imageClickerz( )
          }, 800); 
        });
   }, 100); 
});
  • 写回答

5条回答 默认 最新

  • csdnceshi62 2015-01-06 01:07
    关注

    Make a variable var hasSeenAlert = false before all those functions. Then set up your mousedown function callback to only execute if hasSeenAlert is false.

    $('#sb-wrapper-inner, #sb-nav-previous, #sb-nav-next').mousedown(function(event) { 
        if(hasSeenAlert === false){
            switch (event.which) {
            case 3:
                    alert('Sorry, our images are copyrighted.');
                    break;
            }
            hasSeenAlert = true
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥100 求ASMedia ASM1184e & ASM1187e 芯片datasheet/规格书
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL