模态窗口每天仅对每个访客打开一次

时间:2018-12-23 02:30:14

标签: javascript html caching cookies modal-dialog

我创建了一个模态窗口,几秒钟后会向网站访问者显示该窗口,以通知新消息。

final result screenshot shown here

And this is the temporary example web page

我使用的代码如下:

<div id="ex1" class="modal">
<h1 style="color:#395CC3">We need you !</h1><br>
<p>Lots of text...</p>
<div align="right"> <a href="mailto:contatti@andreadd.it" style="color: 
#395CC3; text-decoration:none">CONTATTACI !!!</a></div>
</div>

<script type="text/javascript">
setTimeout(function(event) {
  $('#ex1').modal({
    fadeDuration: 500,
    fadeDelay: 1.50,
    escapeClose: false,
  });
}, 10000);
</script>

该代码可以正常工作,但是我想将此代码放在所有网站页面上。 我需要,如果将弹出窗口显示给访问者,那么至少一天之内就不会再对该访问者显示。

您能帮我添加一部分代码吗? 非常感谢你!

1 个答案:

答案 0 :(得分:0)

您可以使用localStorage存储上次显示的时间。 LocalStorage w3schools

public class ProdukLaundry extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

ActionBar actionBar;
ListView listProduk;
SwipeRefreshLayout swipeProduct;
List<ProductModel> productList = new ArrayList<ProductModel>();
ProductAdapter productAdapter;
int success;
AlertDialog.Builder dialog;
RadioButton radioReguler, radioExpress;
TextView tvTotal;
Button next;
String product_id, laundry_id, product_name, product_price, service_type;
private int offset = 0;
private static final String TAG = ProdukLaundry.class.getSimpleName();

private static String url_select     = Server.URL + "selectproduk.php";

public static final String TAG_PRODUCT_ID    = "product_id";
public static final String TAG_LAUNDRY_ID    = "laundry_id";
public static final String TAG_PRODUCT_NAME  = "product_name";
public static final String TAG_PRODUCT_PRICE = "product_price";
public static final String TAG_SERVICE_TYPE  = "service_type";
private static final String TAG_SUCCESS      = "success";
private static final String TAG_MESSAGE      = "message";

int countcheckBox = 0;
int totalCount = 0;
boolean regular = true;
boolean express = false;
boolean checkBox = false;

Transaction transaction;

String tag_json_obj = "json_obj_req";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_produk);

    // menghubungkan variablel pada layout dan pada java
    listProduk   = (ListView)findViewById(R.id.list_produk);
    swipeProduct = (SwipeRefreshLayout)findViewById(R.id.swipeProduct);
    radioExpress = (RadioButton)findViewById(R.id.radio_express);
    radioReguler = (RadioButton)findViewById(R.id.radio_regular);
    tvTotal      = (TextView)findViewById(R.id.total);
    next         = (Button)findViewById(R.id.button_next);
    actionBar    = getSupportActionBar();

    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);

    laundry_id = getIntent().getStringExtra(TAG_LAUNDRY_ID);

    // untuk mengisi data dari JSON ke dalam adapter
    productAdapter = new ProductAdapter(ProdukLaundry.this, productList);
    listProduk.setAdapter(productAdapter);

    // menampilkan widget refresh
    swipeProduct.setOnRefreshListener(this);

    swipeProduct.post(new Runnable() {
                   @Override
                   public void run() {
                       swipeProduct.setRefreshing(true);
                       productList.clear();
                       productAdapter.notifyDataSetChanged();
                       callProduct();
                   }
               }
    );

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go home
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onRefresh() {
    productList.clear();
    productAdapter.notifyDataSetChanged();
    callProduct();
}

public void onClick(View view){
    switch (view.getId()){
        case R.id.increase_product:
            countcheckBox++;
            changeCheckBox();
            hitung();
            break;

        case R.id.decrease_product:
            if (countcheckBox>0){
                countcheckBox--;
                changeCheckBox();
                hitung();
            }
            break;

        case R.id.button_next:
            save();
            break;
        default:
            break;
    }
}

public void onRadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();

    switch(view.getId()) {
        case R.id.radio_regular:
            if (checked) {
                regular = true;
                express = false;
                hitung();
            }
            break;
        case R.id.radio_express:
            if (checked){
                regular = false;
                express = true;
                hitung();
            }
            break;
    }
}

public void changeCheckBox(){
    tvTotal.setText(countcheckBox+"");
}

public void hitung(){
    int totalCount = 0;
    int expressCost = 20000;
    int harga = Integer.parseInt(product_price);

    if (checkBox){
        if (express){
            totalCount+=(harga+expressCost)*countcheckBox;
        }else{
            totalCount+=harga*countcheckBox;
        }
    }

    this.totalCount = totalCount;
    tvTotal.setText(this.totalCount+"");
}

private void save() {
    if (tvTotal.getText().toString().equals("0")){
        Toast.makeText(this, "Choose Service.",
                Toast.LENGTH_SHORT).show();
        return;
    }

    String transId = "variabel transaksi id";
    String uid = "variabel produk id";
    String type;
    if (regular) {
        type = "regular";
    } else {
        type = "express";
    }

    transaction = new Transaction(transId, uid, type);
    if (checkBox) {
        transaction.setCheckBox(String.valueOf(countcheckBox));
    }
    transaction.total = String.valueOf(totalCount);

    /*Intent intent = new Intent(this, LocationActivity.class);
    intent.putExtra("transaction", transaction);
    startActivity(intent);*/

}